getting ConnectionCloseException inconsistently

Viewed 86

Getting a ConnectionCloseException when using DefaultHttpClient to call an api. But getting ConnectionCloseException inconsistently.

public static String getResponse(String url) {

    String results = null;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {

        HttpGet getRequest = new HttpGet(url);
        getRequest.addHeader(ACCEPT, APPLICATION_JSON);
        HttpResponse httpResponse = httpClient.execute(getRequest);
        BufferedReader br = new BufferedReader(new 
             InputStreamReader((httpResponse.getEntity().getContent())));
        String line;
        StringBuffer responseBody = new StringBuffer();

            while ((line = br.readLine()) != null) {
                responseBody.append(line);
            }
        results = responseBody.toString());            
        br.close();        
    } catch (Exception e) {
        logger.error(e.toString(), e);
    }
    httpClient.getConnectionManager().shutdown();
    return results;
}

Here's the stack trace

org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 1443; received: 1148
    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:184) ~[httpcore.jar:4.2.5]
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138) ~[httpclient.jar:4.2.6]
    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) 

In all instances, the received bytes were either 0 or 1148, i.e

org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 1443; received: 1148)

or

org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 1359; received: 1148)

Any help is much appreciated. Thanks in advance.

0 Answers
Related