Premature end of message body with Content-Length delimiter (expected:

I am trying to get an HTTP response using apache httpclient. I get headers successfully, but this throws an exception when I try to get the contents. The 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:

 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 would be appreciated. thanks

+14
source share
2 answers

The problem arises on server side, but not in the client pasted by you.

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

+15
source share

I could answer that late. But I also face the same problem. And I got permission from this. In my case, I closed the client before using HttpEntity. And after closing the client, I tried to download the file. The code below is similar to what I was doing:

 HttpEntity httpEntity = null; try (final CloseableHttpClient client = createHttpClient()) { httpEntity = getEntity(client); } return downloadFile(httpEntity, targetDirectory, fileName); 

After setting up my code to download the file before closing the client, now it works for me. The code below is similar to what I did now:

 try (final CloseableHttpClient client = createHttpClient()) { HttpEntity httpEntity = getEntity(client); return downloadFile(httpEntity, targetDirectory, fileName); } 
+12
source share

All Articles