/**
* 获取网页信息
* @param remoteFile 网址url
* @return
* @throws IOException
*/
public String getRemoteHtml(String remoteFile) throws IOException{
URL url = null;
HttpURLConnection urlc = null;
String sCurrentLine="";
java.io.BufferedReader l_reader=null;
StringBuffer sTotalString = new StringBuffer("");
try {
remoteFile=remoteFile.replaceAll("\\s", "%20");//转换空格
System.out.println("search ulr:"+remoteFile);
url = new URL(remoteFile);
urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("Accept-Language", "zh-cn");
// urlc.setRequestProperty("User-Agent","Mozilla/6.0 (compatible; MSIE 5.0; Windows NT; DigExt)");//在linux下需注释该行
// 输入流
l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(urlc.getInputStream(),"UTF-8"));
//得到返回的信息
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString.append(sCurrentLine);
sTotalString.append("\n");
}
}finally{
if(l_reader!=null)
l_reader.close();
if(urlc!=null)
urlc.disconnect();
}
return sTotalString.toString();
}
HttpURLConnection抓取网页响应信息
最新推荐文章于 2022-01-25 21:17:59 发布