您的位置:首页 > 编程语言 > Java开发

Java 獲取網站源碼

2016-03-23 21:34 495 查看
複製可用。

/**
* 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: