Advertisement

android根据Url获取访问网页的源码

阅读量:
复制代码
    /** * 获取HTML数据
     * * */publicclass HtmlService {
    
    publicstaticthrows Exception {
        // 通过网络地址创建URL对象new URL(path);
        // 根据URL
        // 打开连接,URL.openConnection函数会根据URL的类型,返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection (HttpURLConnection) url.openConnection();
        // 设定URL的请求类别,有POST、GET 两类);
        //设置从主机读取数据超时(单位:毫秒));
        //设置连接主机超时(单位:毫秒));
        // 通过打开的连接读取的输入流,获取html数据 conn.getInputStream();
        // 得到html的二进制数据byte readInputStream(inStream);
        // 是用指定的字符集解码指定的字节数组构造一个新的字符串new);
        return html;
    }
    
    /** * 读取输入流,得到html的二进制数据
     * * @param inStream
     * @return
     * @throws Exception
     */publicstaticbytethrows Exception {
        ByteArrayOutputStream outStream new ByteArrayOutputStream();
        bytenewbyte];
        int;
        while) {
            outStream.write(buffer, , len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
    
    }

全部评论 (0)

还没有任何评论哟~