1

I am making an HTTP Get call using Apache HTTPClient (4.x).

httpResponse = httpClient.execute(call, context);
HttpEntity responseEntity = httpResponse.getEntity();
content = EntityUtils.toString(responseEntity);

It throws the following exception

java.io.EOFException
    at java.util.zip.GZIPInputStream.readUByte(GZIPInputStream.java:268)
    at java.util.zip.GZIPInputStream.readUShort(GZIPInputStream.java:258)
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
    at org.apache.http.client.entity.GZIPInputStreamFactory.create(GZIPInputStreamFactory.java:61)
    at org.apache.http.client.entity.LazyDecompressingInputStream.initWrapper(LazyDecompressingInputStream.java:51)
    at org.apache.http.client.entity.LazyDecompressingInputStream.read(LazyDecompressingInputStream.java:69)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.Reader.read(Reader.java:140)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:227)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:270)

After digging into response, I noticed that the server is not sending Content Length header (probably because of Transfer-Encoding: chunked). I also noticed that the response code is 302 Moved Temporarily. I was expecting EntityUtils.toString to return null or empty response. but it throws an exception.

Is this an expected behavior? or do I have to add additional handling? This code was in the common framework and was working for all other HTTP calls.

Response headers:

HttpResponseProxy{
HTTP/1.1 302 Moved Temporarily [
Vary: Accept, Accept-Encoding, 
Set-Cookie: <cookie>; Version=1; Expires=Fri, 06-Aug-2021 22:43:02 GMT; Max-Age=31536000; Domain=.openx.net; Path=/, 
Server: OXGW/16.191.0, 
P3P: CP="CUR ADM OUR NOR STA NID", 
Location: <redirect URL>, 
Date: Thu, 06 Aug 2020 22:43:02 GMT, 
Content-Type: text/html, Via: 1.1 google, Alt-Svc: clear, 
Transfer-Encoding: chunked] org.apache.http.client.entity.DecompressingEntity@68ef95e1}

Response Entity:-

ResponseEntityProxy{[Content-Type: text/html,Content-Encoding: gzip,Chunked: true]}
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Heisenberg
  • 1,500
  • 3
  • 18
  • 35
  • any breakthrough? While reading from the stream as well its not returning -1 but throwing EOF in my case. From [docs](https://docs.oracle.com/javase/8/docs/api/java/io/EOFException.html) , I got that _**"Note that many other input operations return a special value on end of stream rather than throwing an exception"**_. – Mandar Autade May 08 '21 at 18:17

0 Answers0