`
jayyanzhang2010
  • 浏览: 380250 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

HttpCLient实现对被GZip压缩过的Response进行解压

 
阅读更多

发送请求(要求服务端对response进行GZip压缩):

Java代码  收藏代码
  1. import org.apache.commons.httpclient.HttpClient;  
  2. import org.apache.commons.httpclient.HttpStatus;  
  3.   
  4. public class TestGzip {  
  5.   
  6.     private final static String url = "http://localhost:8888/ltest.jsp";  
  7.   
  8.     public static void main(String[] args) throws Exception{  
  9.         HttpClient http = new HttpClient();  
  10.         CustomGetMethod get = new CustomGetMethod(url);  
  11.           
  12.        //添加头信息告诉服务端可以对Response进行GZip压缩  
  13.         get.setRequestHeader("Accept-Encoding""gzip, deflate");  
  14.         try {  
  15.             int statusCode = http.executeMethod(get);  
  16.             if (statusCode != HttpStatus.SC_OK) {  
  17.                 System.err.println("Method failed: "  
  18.                         + get.getStatusLine());  
  19.             }  
  20.   
  21.             //打印解压后的返回信息  
  22.             System.out.println(get.getResponseBodyAsString());  
  23.         } catch (Exception e) {  
  24.             System.err.println("页面无法访问");  
  25.             e.printStackTrace();  
  26.         } finally {  
  27.         get.releaseConnection();  
  28.         }  
  29.     }  
  30. }  

 

下面是CustomGetMethod.java的内容,getResponseBodyAsString()方法被重写,加入了解压功能

Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.InputStreamReader;  
  4. import java.util.zip.GZIPInputStream;  
  5.   
  6.   
  7.   
  8. public class CustomGetMethod extends org.apache.commons.httpclient.methods.GetMethod{  
  9.       
  10.     public CustomGetMethod(String uri) {  
  11.         super(uri);  
  12.     }  
  13.   
  14.       
  15.     /** 
  16.      * Get response as string whether response is GZipped or not 
  17.      *  
  18.      * @return 
  19.      * @throws IOException 
  20.      */  
  21.     @Override  
  22.     public String getResponseBodyAsString() throws IOException {  
  23.         GZIPInputStream gzin;  
  24.         if (getResponseBody() != null || getResponseStream() != null) {  
  25.               
  26.             if(getResponseHeader("Content-Encoding") != null  
  27.                      && getResponseHeader("Content-Encoding").getValue().toLowerCase().indexOf("gzip") > -1) {  
  28.                     //For GZip response  
  29.                     InputStream is = getResponseBodyAsStream();  
  30.                     gzin = new GZIPInputStream(is);  
  31.                       
  32.                     InputStreamReader isr = new InputStreamReader(gzin, getResponseCharSet());   
  33.                     java.io.BufferedReader br = new java.io.BufferedReader(isr);  
  34.                     StringBuffer sb = new StringBuffer();  
  35.                     String tempbf;  
  36.                     while ((tempbf = br.readLine()) != null) {  
  37.                         sb.append(tempbf);  
  38.                         sb.append("\r\n");  
  39.                     }  
  40.                     isr.close();  
  41.                     gzin.close();  
  42.                     return sb.toString();  
  43.                 }  else {  
  44.                 //For deflate response  
  45.                 return super.getResponseBodyAsString();  
  46.             }  
  47.         } else {  
  48.             return null;  
  49.         }  
  50.     }  
  51.   
  52. }  
分享到:
评论

相关推荐

    【ASP.NET编程知识】.Net Core HttpClient处理响应压缩详细.docx

    在这个例子中,我们设置了HttpClientHandler的`AutomaticDecompression`属性为`DecompressionMethods.GZip`,这样HttpClient就会自动处理GZip格式的压缩响应。 二、HttpClientHandler和DecompressionMethods枚举 ...

    decompress:解压用 gzip 压缩的响应体

    去取解压 解压缩用 gzip 压缩的响应体。安装 npm install --save go-fetch-decompress用法 var HttpClient = require('go-fetch');var decompress = require('go-fetch-decompress');var parseBody = require('go-...

    java抓取网页源码gzip-phproxy:PHP中的代理

    标题 "java抓取网页源码gzip-phproxy" 暗示了我们正在讨论一个使用Java进行Web抓取,并处理GZIP压缩的项目,而“PHP中的代理”则可能指的是使用PHP构建的代理服务器。这个项目可能涉及到如何通过Java从通过PHP代理...

    PyPI 官网下载 | EasyConnect-1.0.4.tar.gz

    这种格式结合了tar(归档)和gzip(压缩)两种工具,能够有效地减小文件体积,便于在网络上传输和存储。在下载完成后,我们需要先解压这个文件,然后通过Python的setuptools或pip工具进行安装。 解压"EasyConnect-...

    android省电文档

    3. **使用GZIP压缩进行文本数据传输**: - 使用GZIP压缩可以显著减小传输的数据量,从而降低网络传输消耗的电量。可以通过Java自带的`GZIPInputStream`类来实现数据的解压: ```java ...

    C#如何解析http报文

    5. **处理压缩内容**:如果内容被压缩,比如使用gzip,我们可以使用`System.IO.Compression.GZipStream`来解压。解压后的内容再按照指定的字符集进行解码。 6. **处理编码**:对于非ASCII字符,我们需要使用合适的...

    C#常用的42个类

    29. `System.IO.Compression.GZipStream` 和 `System.IO.Compression.DeflateStream`:GZIP和Deflate压缩流,用于数据压缩和解压。 30. `System.Net.Sockets.TcpClient` 和 `System.Net.Sockets.TcpListener`:TCP...

    在Windows Universal App中使用Web服务

    8. **性能优化**:减少网络请求次数,合并多个请求为一个(如批量操作),使用缓存策略,以及在可能的情况下使用GZIP压缩以减小数据传输量。 通过阅读"Consume-Webservice-in-Windows-Universal-App.pdf"和解压...

Global site tag (gtag.js) - Google Analytics