问题描述
我正在尝试RestAssured&编写了以下语句-
I am trying out RestAssured & wrote the following statements -
String URL = "http://XXXXXXXX";
Response result = given().
header("Authorization","Basic xxxx").
contentType("application/json").
when().
get(url);
JsonPath jp = new JsonPath(result.asString());
在最后一条声明中,我收到以下异常:
On the last statement, I am receiving the following exception :
org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
我的响应中返回的标头是:
The headers returned in my response are :
Content-Type → application/json; qs=1Date → Tue, 10 Nov 2015 02:58:47 GMTTransfer-Encoding → chunked
Content-Type → application/json; qs=1Date → Tue, 10 Nov 2015 02:58:47 GMTTransfer-Encoding → chunked
有人可以指导我解决此异常吗?如果我缺少任何东西或任何不正确的实现,请指出我.
Could anyone guide me on resolving this exception & point me out if I am missing anything or any in-correct implementation.
推荐答案
我遇到了与 rest-assured ,但这是Google发现的第一个结果,因此我将答案发布在这里,以防其他人遇到相同的问题.
I had a similar issue that wasn't related to rest-assured, but this was the first result Google found so I'm posting my answer here, in case other's face the same issue.
对我来说,问题是(如ConnectionClosedException
明确指出的)closing
在读取响应之前的连接.类似于以下内容:
For me, the problem was (as ConnectionClosedException
clearly states) closing
the connection before reading the response. Something along the lines of:
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!
修复很明显.排列代码,以便在关闭响应后不再使用该响应:
The Fix is obvious. Arrange the code so that the response isn't used after closing it:
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();
}
这篇关于org.apache.http.ConnectionClosedException:块编码消息正文的提早结束:预期关闭块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!