11

我正在尝试 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

任何人都可以指导我解决此异常并指出我是否遗漏任何内容或任何不正确的实现。

4

2 回答 2

4

无关的类似问题,但这是谷歌发现的第一个结果,所以我在这里发布我的答案,以防其他人面临同样的问题。

对我来说,问题是(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();
}

于 2019-07-31T14:22:59.083 回答
-1

也许您可以尝试摆弄连接配置?例如:

given().config(RestAssured.config().connectionConfig(connectionConfig().closeIdleConnectionsAfterEachResponse())). ..
于 2015-11-10T16:16:37.643 回答