You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by llem <al...@yahoo.co.uk> on 2005/02/18 19:41:13 UTC

can httpclient handle gzip compressed reponses?

does anyone know whether or not httpclient (version 2 or 3) can decode 
gzip-compressed responses from a server?

or does the application need to be compression-aware, and do the 
decoding itself after calling getResponseBodyAsStream()?

any help appreciated...thanks!
aleem.

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: can httpclient handle gzip compressed reponses?

Posted by llem <al...@yahoo.co.uk>.
for anyone who might be interested in a solution to this problem, here's
some code which seems to do the trick:

/*
  * GZIPAwarePostMethod.java
  *
  * Created on February 21, 2005, 5:25 PM
  *
  */

import java.io.IOException;
import java.util.zip.GZIPInputStream;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;

/**
  *
  * @author aleem
  */
public class GZIPAwarePostMethod extends PostMethod {

     /** Creates a new instance of GZIPAwarePostMethod */
     public GZIPAwarePostMethod() {
         super();
     }

     /**
      * Constructor specifying a URI.
      *
      * @param uri either an absolute or relative URI
      *
      * @since 1.0
      */
     public GZIPAwarePostMethod(String uri) {
         super(uri);
     }

     /**
      * Constructor specifying a URI and a tempDir.
      *
      * @param uri either an absolute or relative URI
      * @param tempDir directory to store temp files in
      *
      * @deprecated the client is responsible for disk I/O
      * @since 1.0
      */
     public GZIPAwarePostMethod(String uri, String tempDir) {
         super(uri, tempDir);
     }

     /**
      * Constructor specifying a URI, tempDir and tempFile.
      *
      * @param uri either an absolute or relative URI
      * @param tempDir directory to store temp files in
      * @param tempFile file to store temporary data in
      *
      * @deprecated the client is responsible for disk I/O
      * @since 1.0
      */
     public GZIPAwarePostMethod(String uri, String tempDir, String tempFile) {
         super(uri, tempDir, tempFile);
     }

     /**
      * Overrides method in {@link HttpMethodBase}.
      *
      * Notifies the server that we can process a GZIP-compressed response before
      * sending the request.
      *
      */
     public int execute(HttpState state, HttpConnection conn) throws HttpException, HttpRecoverableException, 
IOException {
         // Tell the server that we can handle GZIP-compressed data in the response body
         addRequestHeader("Accept-Encoding", "gzip");

         return super.execute(state, conn);
     }

     /**
      * Overrides method in {@link GetMethod} to set the responseStream variable appropriately.
      *
      * If the response body was GZIP-compressed, responseStream will be set to a GZIPInputStream
      * wrapping the original InputStream used by the superclass.
      *
      */
     protected void readResponseBody(HttpState state, HttpConnection conn) throws IOException, HttpException {
         super.readResponseBody(state, conn);

         Header contentEncodingHeader = getResponseHeader("Content-Encoding");

         if (contentEncodingHeader != null && contentEncodingHeader.getValue().equalsIgnoreCase("gzip"))
             setResponseStream(new GZIPInputStream(getResponseStream()));
     }

}

cheers,
aleem.

-------------

Oleg Kalnichevski wrote:
> Aleem,
> 
> HttpClient is content agnostic per design. Application specific content
> handling should be built on top of HttpClient services
> 
> Oleg
> 
> On Fri, 2005-02-18 at 13:41 -0500, llem wrote:
> 
>>does anyone know whether or not httpclient (version 2 or 3) can decode 
>>gzip-compressed responses from a server?
>>
>>or does the application need to be compression-aware, and do the 
>>decoding itself after calling getResponseBodyAsStream()?
>>
>>any help appreciated...thanks!
>>aleem.
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>>
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: can httpclient handle gzip compressed reponses?

Posted by Oleg Kalnichevski <ol...@apache.org>.
Aleem,

HttpClient is content agnostic per design. Application specific content
handling should be built on top of HttpClient services

Oleg

On Fri, 2005-02-18 at 13:41 -0500, llem wrote:
> does anyone know whether or not httpclient (version 2 or 3) can decode 
> gzip-compressed responses from a server?
> 
> or does the application need to be compression-aware, and do the 
> decoding itself after calling getResponseBodyAsStream()?
> 
> any help appreciated...thanks!
> aleem.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org