1.DefaultHttpClient 使用GZIPInputStream解压缩:
DefaultHttpClient 使用GZIPInputStream解压缩,当浏览器访问网站时,有可能浏览器返回的消息头中带有 Content-Encoding:gzip,表明服务器返回的消息经过gzip压缩,这么做是为了节省流量,浏览器拿到gzip压缩后的http包,对其进行解压缩,再渲染出来。在使用apache提供的DefaultHttpClient操作http请求时,可以使用GZIPInputStream对gzip压缩过的数据包进行解压缩。android sdk进行网络编程时,也可以使用这种方法。简单代码如下:
DefaultHttpClient httpClient = new DefaultHttpClient();
//dns探测源ip
String[] dnsIps = {"jsdx", "yndx", "bjdx", "bjlt", "sclt", "shlt", "gg"};
for(String ip : dnsIps)
{
HttpPost post = new HttpPost("http://webscan.360.cn/tools/dnsInfo.php");
post.addHeader("Referer", "http://webscan.360.cn/tools/dnslookup");
post.addHeader("X-Requested-With", "XMLHttpRequest");
post.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
post.addHeader("Accept", "*/*");
post.addHeader("Accept-Encoding", "gzip, deflate");
post.addHeader("Accept-Language", "zh-cn,en-us;q=0.7,en;q=0.3");
post.addHeader("Pragma", "no-cache");
post.addHeader("Cache-Control", "no-cache");
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("dns_ip", ip));
params.add(new BasicNameValuePair("domain_name", domain));
params.add(new BasicNameValuePair("dns_type", "A"));
UrlEncodedFormEntity formEntity = null;
HttpResponse response = null;
String responseHtml = null;
try
{
formEntity = new UrlEncodedFormEntity(params, "utf-8");
post.setEntity(formEntity);
response = httpClient.execute(post);
InputStream is= response.getEntity().getContent();
is= new GZIPInputStream(is);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while((line = br.readLine())!=null) {
sb.append(line);
}
responseHtml = sb.toString();
} catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
2. httpclient默认是不会对gzip格式解压的,需要手动处理一下,代码如下:
- DefaultHttpClient httpclient = new DefaultHttpClient();
-
- httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
-
- public void process(
- final HttpRequest request,
- final HttpContext context) throws HttpException, IOException {
- if (!request.containsHeader("Accept-Encoding")) {
- request.addHeader("Accept-Encoding", "gzip");
- }
- }
-
- });
-
- httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
-
- public void process(
- final HttpResponse response,
- final HttpContext context) throws HttpException, IOException {
- HttpEntity entity = response.getEntity();
- Header ceheader = entity.getContentEncoding();
- if (ceheader != null) {
- HeaderElement[] codecs = ceheader.getElements();
- for (int i = 0; i < codecs.length; i++) {
- if (codecs[i].getName().equalsIgnoreCase("gzip")) {
- response.setEntity(
- new GzipDecompressingEntity(response.getEntity()));
- return;
- }
- }
- }
- }
-
- });
-
- HttpGet httpget = new HttpGet("http://www.apache.org/");
-
-
- System.out.println("executing request " + httpget.getURI());
- HttpResponse response = httpclient.execute(httpget);
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- System.out.println(response.getLastHeader("Content-Encoding"));
- System.out.println(response.getLastHeader("Content-Length"));
- System.out.println("----------------------------------------");
-
- HttpEntity entity = response.getEntity();
-
- if (entity != null) {
- String content = EntityUtils.toString(entity);
- System.out.println(content);
- System.out.println("----------------------------------------");
- System.out.println("Uncompressed size: "+content.length());
- }
-
-
-
-
- httpclient.getConnectionManager().shutdown();