tags:

views:

317

answers:

3

Hi Can any one of you solve this problem !

Problem Description:

i have received content-encoding: gzip header from http web-server. now i want to decode the content but when i use GZIP classes from jdk 1.6.12, it gives null.

does it means that contents are not in gzip format ? or are there some another classes for decompress http response content?

Sample Code:

System.out.println("Reading InputStream");
InputStream in = httpuc.getInputStream();// httpuc is an object of httpurlconnection
System.out.println("Before reading GZIP inputstream");
System.out.println(in);
GZIPInputStream gin = new GZIPInputStream(in));
System.out.println("After reading GZIP inputstream");

Output:

Reading InputStream
Before reading GZIP inputstream
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@8acf6e
null

I have found one error in code, but don't able to understand it properly. what does it indicates.

Error ! java.io.EOFException

Thanks

A: 

The expression new GZIPInputStream(in) can never return null - a constructor call can only return a new object or throw an exception.

Could you post a short but complete program to demonstrate the problem? Or at the very least part of your code and why you think something is null?

Jon Skeet
Please edit your question rather than posting code in comments. That still doesn't show what you're testing for nullity.
Jon Skeet
+2  A: 

I think you should have a look at HTTPClient, which will handle a lot of the HTTP issues for you. In particular, it allows access to the response body, which may be gzipped, and then you simply feed that through a GZIPInputStream

e.g.

    Header hce = postMethod.getResponseHeader("Content-Encoding");
    InputStream in = null;
    if(null != hce)
    {
     if(hce.getValue().equals(GZIP)) {
        in = new GZIPInputStream(postMethod.getResponseBodyAsStream());
     }
         // etc...
Brian Agnew
A: 

I second Brian's suggestion. Whenever u need to deal with getting/posting stuff via HTTP don't bother with low-level access use the Apache HTTP client.

stwissel