org.apache.http.ConnectionClosedException:块编码消息体的提前结束:关闭块预期

时间:2015-11-10 15:28:07

标签: apache http rest-assured chunked-encoding rest-assured-jsonpath

我正在尝试RestAssured&写了以下陈述 -

String URL = "http://XXXXXXXX";
Response result = given().
            header("Authorization","Basic xxxx").
            contentType("application/json").
            when().
            get(url);
JsonPath jp = new JsonPath(result.asString());

在最后一个声明中,我收到以下异常:

org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected

我的回复中返回的标题是:

Content-Type → application/json; qs=1 Date → Tue, 10 Nov 2015 02:58:47 GMT Transfer-Encoding → chunked

任何人都可以指导我解决此异常问题吗?如果我遗漏任何内容或任何不正确的实施,请指出我。

2 个答案:

答案 0 :(得分:0)

也许你可以尝试摆弄connection config?例如:

given().config(RestAssured.config().connectionConfig(connectionConfig().closeIdleConnectionsAfterEachResponse())). ..

答案 1 :(得分:0)

我遇到了与无关的类似问题,但这是Google发现的第一个结果,因此我将答案发布在这里,以防其他人遇到相同的问题。

对我来说,问题是(如ConnectionClosedException明确指出)closing是连接,然后才读取响应。类似于:

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);

try {
    doSomthing();
} finally {
    response.close();
}
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent(); // Response already closed. This won't work!

修复很明显。排列代码,以便在关闭响应后不使用响应:

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);

try {
    doSomthing();
    HttpEntity entity = response.getEntity();
    InputStream instream = entity.getContent(); // OK
} finally {
    response.close();
}