29

I am trying to get HTTP response with the help of apache httpclient. I get headers successfully but it throws exception when I try to get contents. Exception is:

 org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 203856; received: 1070
        at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:180)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:154)
        at java.io.BufferedReader.readLine(BufferedReader.java:317)
        at java.io.BufferedReader.readLine(BufferedReader.java:382)

and my code is:

InputStream is = entity.getContent();
BufferedReader br = new BufferedReader( new InputStreamReader(is, "UTF-8"));
String line;
String str = "";
while ((line = br.readLine()) != null) {

    str = str + line + "\n";

}
log.debug(str);

any help will be appreciated. thanks

Khanjee
  • 293
  • 1
  • 3
  • 6

5 Answers5

36

I might be replying on it late. But I also encounter the same problem. And I got the resolution of it. In my case I was closing the client before utilizing the HttpEntity. And after closing the client I was trying to download the file. Below code is similar to what I was doing:

HttpEntity httpEntity = null;
try (final CloseableHttpClient client = createHttpClient()) {
     httpEntity = getEntity(client);
}

return downloadFile(httpEntity, targetDirectory, fileName);

After adjusting my code to download the file before closing the client, Its working now for me. Below code is similar to what I did now:

try (final CloseableHttpClient client = createHttpClient()) {
     HttpEntity httpEntity = getEntity(client);
     return downloadFile(httpEntity, targetDirectory, fileName);
}
Nitin Singhal
  • 591
  • 5
  • 6
  • How can I do it after closing the client? I have to use the input stream of response in some other service layer – Arun Gowda May 27 '19 at 06:43
22

The problem appears to be on the server-side, not in the client code you've pasted.

The server claimed that the content contained 203856 bytes but only sent 1070.

Daniel Renshaw
  • 33,729
  • 8
  • 75
  • 94
  • In my case it is `expected: 548843; received: 536363`, I can see the difference is very minute. What could be the reason? Is there any way to overcome or ignore it? https://imgur.com/ywGwvVQ is my client code – subject-q Jul 15 '19 at 06:50
  • @daniel-renshaw I agree, I was getting the same error. Upon checking the Server-side logs, I came to know that it was coming due to an exception on server side. In my case it was OutOfMemory error on server-side endpoint. Hence, this is not a client issue. – kulsin Oct 18 '20 at 17:35
3

I was closing the stream before returning it enter image description here

Chris Gunawardena
  • 6,246
  • 1
  • 29
  • 45
2

I got the same issue while downloading the file from Jfrog Artifactory from jenkins and Changing the Nginx reverse proxy config value proxy_max_temp_file_size to 0solved my issue.

SNR
  • 460
  • 5
  • 20
  • What gave you the idea that this was the error? (It was mine as well, but other than your comment I had no clue) – powerj1984 Jul 08 '22 at 14:41
0

I encountered a similar error and solved it by persisting my HTTP connection for a few extra seconds. Something like Basically add a keep-alive header on the HTTP request for something like 3-5 sec

G. Joe
  • 53
  • 2
  • 15
  • Links may be broken over time, so even if your answer is correct, since it depends on the contents of a link, if the link is broken at some point, then your answer will be useless. To avoid that, you will need to edit your answer and add the main ideas of the solution you propose. – Lajos Arpad May 05 '22 at 10:37