public static String getHtmlContent(String htmlurl, String charset) {
StringBuffer sb = new StringBuffer();
String acceptEncoding = "";
/* 1.生成 HttpClinet 对象并设置参数 */
HttpClient httpClient = new HttpClient();
// 设置 Http 连接超时 5s
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
5000);
GetMethod method = new GetMethod(htmlurl);
// 设置 get 请求超时 5s
method.getParams()
.getDoubleParameter(HttpMethodParams.SO_TIMEOUT, 10000);
// 设置请求重试处理
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
int statusCode;
try {
statusCode = httpClient.executeMethod(method);
// 判断访问的状态码
if (statusCode != HttpStatus.SC_OK) {
return sb.toString();
} else {
if (method.getResponseHeader("Content-Encoding") != null)
acceptEncoding = method
.getResponseHeader("Content-Encoding").getValue();
if (acceptEncoding.toLowerCase().indexOf("gzip") > -1) {
// 建立gzip解压工作流
InputStream is;
is = method.getResponseBodyAsStream();
GZIPInputStream gzin = new GZIPInputStream(is);
InputStreamReader isr = new InputStreamReader(gzin, charset); // 设置读取流的编码格式,自定义编码
java.io.BufferedReader br = new java.io.BufferedReader(isr);
String tempbf;
while ((tempbf = br.readLine()) != null) {
sb.append(tempbf);
sb.append("\r\n");
}
isr.close();
gzin.close();
System.out.println(sb);
} else {
InputStreamReader isr;
isr = new InputStreamReader(
method.getResponseBodyAsStream(), charset);
java.io.BufferedReader br = new java.io.BufferedReader(isr);
String tempbf;
while ((tempbf = br.readLine()) != null) {
sb.append(tempbf);
sb.append("\r\n");
}
isr.close();
}
}
} catch (HttpException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
method.abort();
method.releaseConnection();
return sb.toString();
}