不一致地获取ConnectionCloseException

问题描述

使用ConnectionCloseException调用api时获得DefaultHttpClient。但是获得ConnectionCloseException不一致。

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

这是堆栈跟踪

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) 

在所有情况下,收到的字节均为0或1148,即

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

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

我们非常感谢您的帮助。 预先感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)