ConnectionClosedException:apache hhtpclient 4.5.5的块编码消息正文的过早结束

时间:2019-09-09 09:15:02

标签: spring-boot apache-httpclient-4.x

我正在尝试解析GET请求中的http响应,但是会引发以下异常。

org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
    at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:266) ~[httpcore-4.4.10.jar!/:4.4.10]
    at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:225) ~[httpcore-4.4.10.jar!/:4.4.10]
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:184) ~[httpcore-4.4.10.jar!/:4.4.10]
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135) ~[httpclient-4.5.6.jar!/:4.5.6]
    at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[na:na]
    at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[na:na]
    at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[na:na]
    at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185) ~[na:na]
    at java.base/java.io.Reader.read(Reader.java:229) ~[na:na]
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:227) ~[httpcore-4.4.10.jar!/:4.4.10]
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:308) ~[httpcore-4.4.10.jar!/:4.4.10]

我解析响应的代码是

String parseResponse(HttpResponse resp) {
        try {
            return org.apache.http.util.EntityUtils.toString(resp.getEntity());
        } catch (IOException e) {
                        throw new RuntimeException(e);
        }
    }

我使用了 org.apache.httpcomponents:httpcore:4.5.6

我调用的GET端点(春季启动应用程序)如下

public ResponseEntity<org.springframework.data.domain.
Page<JSONObject>> getList() {
}

我在这里读过https://github.com/jersey/jersey/issues/3629,如果我使用org.apache.httpcomponents:httpclient:4.5.5,那么它应该可以正常工作,但对我却不起作用。

有人可以给我一些指点吗?

我看到了类似的问题org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected,但并没有帮助我。

1 个答案:

答案 0 :(得分:1)

了解该问题。我关闭CloseableHttpClient,然后尝试使用HTTPResponse。 后续帖子帮助了 https://stackoverflow.com/a/50136644/1053496