如何解决不一致地获取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 举报,一经查实,本站将立刻删除。