2

I sent a request for question with no parameters to https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow

I received a response of 9764 bytes stored in StringBuffer.

The StringBuffer was converted to byte[] to deCompress. GZIPInputStream threw an exception:

Not in GZIP format.

Is it necessary to set parameters in the request to get a response in GZIP format? If not, how to inflate the response?

public class MyConnect {
    private static final String GET_URL = "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow";
    private static void sendGET() throws IOException {
        URL obj = new URL(GET_URL);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        
        if (responseCode == HttpURLConnection.HTTP_OK){
            BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while((inputLine = in.readLine()) != null){
                    response.append(inputLine);
            }
            System.out.println("len: "+ response.length());
            System.out.println("bytes: "+ response.toString().getBytes().length);
            in.close();


            String s = deCompress(response.toString().getBytes());
            if (s != null)
                System.out.println(s);
            else 
                System.out.println("response is null");
            
        }else { 
            //ToD:  error handling.
            System.out.println(responseCode);
        }

    }
    public static void main(String[] args){
        
        try{
            MyConnect.sendGET();
        }catch(IOException e){
            System.out.println("CONNECTION FAILED");
        }finally{
            
        }
    }
    
    private static String deCompress(byte[] str) throws IOException{
        StringBuilder sb = null;
    try{
        ByteArrayInputStream bis = new ByteArrayInputStream(str);
        GZIPInputStream gis = new GZIPInputStream(bis);  // *** exception : not in GZIP format
        BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
        sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null){
            sb.append(line);
        }
        br.close();
        gis.close();
        bis.close();
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        ;
    }
    return sb.toString();

}
    }
1
  • 1
    @TheWanderer that's your browser automatically handling a GZIPped response for you :) It is always compressed, according to the documentation.
    – Glorfindel
    Jul 6, 2021 at 10:07

1 Answer 1

2

You're correct in assuming you need dedicated compression support; HttpURLConnection by itself won't be able to process the output. I'm not sure where your problem is, but let me help you with this simple example (using IOUtils from Apache Commons IO). The deCompress method isn't even necessary:

if (responseCode == HttpURLConnection.HTTP_OK) {
    String s;
    try (InputStream inputStream = con.getInputStream();
            GZIPInputStream gzipStream = new GZIPInputStream(inputStream)) {
        s = IOUtils.toString(gzipStream);
    }
    if (s != null)
        System.out.println(s);
    else
        System.out.println("response is null");
}

It prints:

{"items":[{"tags":["python","pandas"],"owner":{"reputation":1,"user_id":14202979,"user_type":"registered","profile_image":"https://lh5.googleusercontent.com/-vL7cQ1m3llk/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnoY4-nJ1QaoQxp4f9T5tyqcZfR8Q/photo.jpg?sz=128","display_name":"Gerard","link":"https://stackoverflow.com/users/14202979/gerard"},...

Not the answer you're looking for? Browse other questions tagged .