GetMethod method = new GetMethod(url);
method.setRequestHeader("Accept-Encoding", "gzip, deflate");
try {
/* 获得信息载体 */
int state = getHttpClient().executeMethod(method);
if (state == 200) {
// content = method.getResponseBodyAsString();
InputStream is = method.getResponseBodyAsStream();
GZIPInputStream gzin = new GZIPInputStream(is);
InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 设置读取流的编码格式,自定义编码
java.io.BufferedReader br = new java.io.BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String tempbf;
while ((tempbf = br.readLine()) != null) {
sb.append(tempbf);
sb.append("\r\n");
}
isr.close();
gzin.close();
String html = sb.toString();
System.out.println(html);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
}
爬取gzip加密的网页
最新推荐文章于 2024-08-29 16:51:49 发布