Advertisement
Guest User

httpclientgzip

a guest
Jun 1st, 2011
1,924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.util.zip.GZIPInputStream;
  4.  
  5. import org.apache.http.Header;
  6. import org.apache.http.HeaderElement;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpException;
  9. import org.apache.http.HttpRequest;
  10. import org.apache.http.HttpRequestInterceptor;
  11. import org.apache.http.HttpResponse;
  12. import org.apache.http.HttpResponseInterceptor;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.entity.HttpEntityWrapper;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.apache.http.protocol.HttpContext;
  17. import org.apache.http.util.EntityUtils;
  18.  
  19. /**
  20.  * Demonstration of the use of protocol interceptors to transparently
  21.  * modify properties of HTTP messages sent / received by the HTTP client.
  22.  * <p/>
  23.  * In this particular case HTTP client is made capable of transparent content
  24.  * GZIP compression by adding two protocol interceptors: a request interceptor
  25.  * that adds 'Accept-Encoding: gzip' header to all outgoing requests and
  26.  * a response interceptor that automatically expands compressed response
  27.  * entities by wrapping them with a uncompressing decorator class. The use of
  28.  * protocol interceptors makes content compression completely transparent to
  29.  * the consumer of the {@link org.apache.http.client.HttpClient HttpClient}
  30.  * interface.
  31.  */
  32. public class test
  33. {
  34.     public final static void main(String[] args) throws Exception {
  35.         DefaultHttpClient httpclient = new DefaultHttpClient();
  36.  
  37.         httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
  38.            
  39.             public void process(
  40.                     final HttpRequest request,
  41.                     final HttpContext context) throws HttpException, IOException {
  42.                 if (!request.containsHeader("Accept-Encoding")) {
  43.                     request.addHeader("Accept-Encoding", "gzip");
  44.                 }
  45.             }
  46.  
  47.         });
  48.        
  49.         httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
  50.            
  51.             public void process(
  52.                     final HttpResponse response,
  53.                     final HttpContext context) throws HttpException, IOException {
  54.                 HttpEntity entity = response.getEntity();
  55.                 Header ceheader = entity.getContentEncoding();
  56.                 if (ceheader != null) {
  57.                     HeaderElement[] codecs = ceheader.getElements();
  58.                     for (int i = 0; i < codecs.length; i++) {
  59.                         if (codecs[i].getName().equalsIgnoreCase("gzip")) {
  60.                             response.setEntity(
  61.                                     new GzipDecompressingEntity(response.getEntity()));
  62.                             return;
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.            
  68.         });
  69.        
  70.         HttpGet httpget = new HttpGet("http://www.apache.org/");
  71.        
  72.         // Execute HTTP request
  73.         System.out.println("executing request " + httpget.getURI());
  74.         HttpResponse response = httpclient.execute(httpget);
  75.  
  76.         System.out.println("----------------------------------------");
  77.         System.out.println(response.getStatusLine());
  78.         System.out.println(response.getLastHeader("Content-Encoding"));
  79.         System.out.println(response.getLastHeader("Content-Length"));
  80.         System.out.println("----------------------------------------");
  81.  
  82.         HttpEntity entity = response.getEntity();
  83.        
  84.         if (entity != null) {
  85.             String content = EntityUtils.toString(entity);
  86.             System.out.println(content);
  87.             System.out.println("----------------------------------------");
  88.             System.out.println("Uncompressed size: "+content.length());
  89.         }
  90.  
  91.         // When HttpClient instance is no longer needed,
  92.         // shut down the connection manager to ensure
  93.         // immediate deallocation of all system resources
  94.         httpclient.getConnectionManager().shutdown();        
  95.     }
  96.  
  97.     static class GzipDecompressingEntity extends HttpEntityWrapper {
  98.  
  99.         public GzipDecompressingEntity(final HttpEntity entity) {
  100.             super(entity);
  101.         }
  102.    
  103.         @Override
  104.         public InputStream getContent()
  105.             throws IOException, IllegalStateException {
  106.  
  107.             // the wrapped entity's getContent() decides about repeatability
  108.             InputStream wrappedin = wrappedEntity.getContent();
  109.  
  110.             return new GZIPInputStream(wrappedin);
  111.         }
  112.  
  113.         @Override
  114.         public long getContentLength() {
  115.             // length of ungzipped content is not known
  116.             return -1;
  117.         }
  118.  
  119.     }    
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement