A bit confused about HttpClient[Java] handling gzip responses -


my application makes http request api service, service returns gzipped response. how can make sure response indeed in gzip format? i'm confused @ why after making request didn't have decompress it.

below code:

public static string streamtostring(inputstream stream) {     bufferedreader reader = new bufferedreader(new inputstreamreader(stream));     stringbuilder sb = new stringbuilder();     string line;      try {         while ((line = reader.readline()) != null) {             sb.append(line).append("\n");         }     } catch (ioexception e) {         logger.error("error while streaming string: {}", e);     } {         try { stream.close(); } catch (ioexception e) { }     }      return sb.tostring(); }  public static string getresultfromhttprequest(string url) throws ioexception { // add retries, catch exceptions     httpclient httpclient = new defaulthttpclient();     httpget httpget;     httpresponse httpresponse;     inputstream stream;      try {         httpget = new httpget(url);         httpget.setheader("content-encoding", "gzip, deflate");         httpresponse = httpclient.execute(httpget);         logger.info(httpresponse.getentity().getcontentencoding());         logger.info(httpresponse.getentity().getcontent());         if (httpresponse.getstatusline().getstatuscode() == 200) {             stream = httpresponse.getentity().getcontent();             return streamtostring(stream);         }     } catch (illegalstateexception e) {         logger.error("error while trying access: " + url, e);     }      return ""; } 

maybe decompressing automatically, see indication of @ least.

hi late answer might used facing same issue. default content decompressed in response. so, have disable default compression using following code:

closeablehttpclient client = httpclients.custom()     .disablecontentcompression()     .build();  httpget request = new httpget(urlsring); request.setheader(httpheaders.accept_encoding, "gzip");  closeablehttpresponse response = client.execute(request, context); httpentity entity = response.getentity(); header contentencodingheader = entity.getcontentencoding();  if (contentencodingheader != null) {     headerelement[] encodings =contentencodingheader.getelements();     (int = 0; < encodings.length; i++) {         if (encodings[i].getname().equalsignorecase("gzip")) {             entity = new gzipdecompressingentity(entity);             break;         }     } }  string output = entityutils.tostring(entity, charset.forname("utf-8").name()); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -