Skip to main content
Post Closed as "Duplicate" by rene
added 8 characters in body; edited tags; edited tags
Source Link
Glorfindel
  • 6.9k
  • 3
  • 22
  • 49

How to handle default compression on response with Java's HttpURLConnection?

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

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

default compression on response

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

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

How to handle default compression on response with Java's HttpURLConnection?

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

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

Post Migrated Here from meta.stackexchange.com (revisions)
Source Link

default compression on response

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 exception :

Not in GZIP format.

Is it necessary to set parameters in request to get 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();

}
    }