//1. 获取响应返回的信息
//*猫的responseHeader的Content-Encoding=gzip,且Content-Type的charset=GBK
//*东的responseHeaders里没有Content-Encoding,且Content-Type的charset=UTF-8
GetMethod getMethod = new GetMethod(url);
...
InputStream in = getMethod.getResponseBodyAsStream();
BufferedReader reader = null;
Header header = getMethod.getResponseHeader("Content-Encoding");
if (header != null) {//天猫
if ("gzip".equals(header.getValue())) {
GZIPInputStream gis = new GZIPInputStream(in);//使用GZIP包装一下
reader = new BufferedReader(new InputStreamReader(gis, "GBK"));
}
} else {//京东
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
}
StringBuffer buffer = new StringBuffer();
String info = reader.readLine();
while (info != null) {
buffer.append(info);
buffer.append("\n");
info = reader.readLine();
}
responseStr = buffer.toString();