我正在尝试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
我的响应中返回的 header 是:
Content-Type → application/json; qs=1Date → Tue, 10 Nov 2015 02:58:47 GMTTransfer-Encoding → chunked
任何人都可以指导我解决此异常,并在缺少任何内容或任何不正确的实现时指出我。

最佳答案

我遇到了与rest-assured无关的类似问题,但这是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();
}

09-25 21:03