利用httpClient下载页面,先利用浏览器查询页面源代码得知页面编码为utf-8,但生成字符串后总是为乱码,经过查询得知,在设置httpGet参数时,设置了接受压缩类型为Gzip,但却没有对其进行解压缩。解压后在生成字符串,或者这是接受类型为空(即不压缩,效率较低)即可。
第一种:
httpGet.setHeader("Accept-Encoding", "gzip"); //此行注释掉即可!!
或者采用 第二种:
/**
* 处理gzip,deflate返回流
*
* @param is
* @return
* @throws IOException
*/
private String zipInputStream(InputStream is) throws IOException {
GZIPInputStream gzip = new GZIPInputStream(is);
BufferedReader in = new BufferedReader(new InputStreamReader(gzip, "UTF-8"));
StringBuffer buffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null)
buffer.append(line + "\n");
is.close();
return buffer.toString();
}
这种方法对 gzip或deflate流的解析。