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]}