httpClient

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格式解压的,需要手动处理一下,代码如下:
 
  
  
  1. DefaultHttpClient httpclient = new DefaultHttpClient();  
  2.   
  3.        httpclient.addRequestInterceptor(new HttpRequestInterceptor() {  
  4.             
  5.            public void process(  
  6.                    final HttpRequest request,   
  7.                    final HttpContext context) throws HttpException, IOException {  
  8.                if (!request.containsHeader("Accept-Encoding")) {  
  9.                    request.addHeader("Accept-Encoding""gzip");  
  10.                }  
  11.            }  
  12.   
  13.        });  
  14.          
  15.        httpclient.addResponseInterceptor(new HttpResponseInterceptor() {  
  16.             
  17.            public void process(  
  18.                    final HttpResponse response,   
  19.                    final HttpContext context) throws HttpException, IOException {  
  20.                HttpEntity entity = response.getEntity();  
  21.                Header ceheader = entity.getContentEncoding();  
  22.                if (ceheader != null) {  
  23.                    HeaderElement[] codecs = ceheader.getElements();  
  24.                    for (int i = 0; i < codecs.length; i++) {  
  25.                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {  
  26.                            response.setEntity(  
  27.                                    new GzipDecompressingEntity(response.getEntity()));   
  28.                            return;  
  29.                        }  
  30.                    }  
  31.                }  
  32.            }  
  33.              
  34.        });  
  35.          
  36.        HttpGet httpget = new HttpGet("http://www.apache.org/");   
  37.          
  38.        // Execute HTTP request  
  39.        System.out.println("executing request " + httpget.getURI());  
  40.        HttpResponse response = httpclient.execute(httpget);  
  41.   
  42.        System.out.println("----------------------------------------");  
  43.        System.out.println(response.getStatusLine());  
  44.        System.out.println(response.getLastHeader("Content-Encoding"));  
  45.        System.out.println(response.getLastHeader("Content-Length"));  
  46.        System.out.println("----------------------------------------");  
  47.   
  48.        HttpEntity entity = response.getEntity();  
  49.          
  50.        if (entity != null) {  
  51.            String content = EntityUtils.toString(entity);  
  52.            System.out.println(content);  
  53.            System.out.println("----------------------------------------");  
  54.            System.out.println("Uncompressed size: "+content.length());  
  55.        }  
  56.   
  57.        // When HttpClient instance is no longer needed,   
  58.        // shut down the connection manager to ensure  
  59.        // immediate deallocation of all system resources  
  60.        httpclient.getConnectionManager().shutdown();  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值