Java 獲取網站源碼

来源:互联网 发布:js 数组取值 编辑:程序博客网 时间:2024/06/15 17:31

複製可用。

/** * Get a page's source code * @param strUrl Url of this page * @param charset Charset of this page, like "utf-8","gbk" * @return * @throws IOException */public String fetchHtml(String strUrl, String charset) throws Exception{    if (!strUrl.startsWith("http://"))        strUrl = "http://" + strUrl;    URL url = new URL(strUrl);    HttpURLConnection con = (HttpURLConnection) url.openConnection();    InputStream is = con.getInputStream();    InputStreamReader isr = new InputStreamReader(is, charset);    String html = "";    int read;    for (; (read = isr.read()) != -1; )        html += (char) read;    isr.close();    return html;}
0 0
原创粉丝点击