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

发送http请求时,设置请求头Accept-Encoding:gzip, deflate,则服务器会返回压缩的内容。

若不设置,相应内容则正常返回。


发送请求(要求服务端对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.   

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值