Question

I am running MVC 4 on my server and to save a bit of data for my users I figured I would enable GZip encoding, to do this I simply used:

(C#)

Response.AddHeader("Content-Encoding", "gzip"); 
Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);

In my android application I use:

(Java)

String response = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
    HttpResponse execute = client.execute(httpGet);
    InputStream content = execute.getEntity().getContent();

    BufferedReader buffer = new BufferedReader(
        new InputStreamReader(content));
    String s = "";
    while ((s = buffer.readLine()) != null) {
        response += s;
    }

} catch (Exception e) {
    e.printStackTrace();
}
return response;

When I use GZip the Java code nuts out and causes GC to run, I was never patient enough to wait for it to return.

When I took off GZip from the server it ran perfectly fine. The function to get the response returns straight away with no problem.

I tried adding this to the java code:

httpGet.addHeader("Accept-Encoding", "gzip");

With no success.

Question is, is there something I'm not getting? Can I not put the response in a stream if it is using GZip? Am I meant to use the stream and uncompress it after?

What am I doing wrong?

Was it helpful?

Solution

Instead of using

DefaultHttpClient client = new DefaultHttpClient();

you can use

ContentEncodingHttpClient client = new ContentEncodingHttpClient();

which is a subclass of DefaultHttpClient and supports GZIP content. You need Apache HttpClient 4.1 for this.

If you have Apache HttpClient 4.2, you should use

DecompressingHttpClient client = new DecompressingHttpClient();

if you have Apache HttpClient 4.3, you should use the HttpClientBuilder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top