`
jayyanzhang2010
  • 浏览: 372108 次
  • 性别: 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. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics