org.apache.http.ConnectionClosedException:块编码消息正文过早结束:应关闭块
我正在尝试重新发行并撰写以下声明-org.apache.http.ConnectionClosedException:块编码消息正文过早结束:应关闭块,apache,http,rest-assured,chunked-encoding,rest-assured-jsonpath,Apache,Http,Rest Assured,Chunked Encoding,Rest Assured Jsonpath,我正在尝试重新发行并撰写以下声明- String URL = "http://XXXXXXXX"; Response result = given(). header("Authorization","Basic xxxx"). contentType("application/json"). when(). get(url); JsonPath jp = new JsonPath(result.as
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:块编码消息正文过早结束:应关闭块
我的回复中返回的标题是:
内容类型→ 应用程序/json;qs=1
日期→ 2015年11月10日星期二02:58:47 GMT
传输编码→ 分块的
是否有人可以指导我解决此异常,并指出我在正确的实现中是否遗漏了任何内容。也许您可以尝试处理一下?例如:
given().config(RestAssured.config().connectionConfig(connectionConfig().closeIdleConnectionsAfterEachResponse())). ..
我有一个类似的问题与此无关,但这是谷歌发现的第一个结果,所以我在这里发布我的答案,以防其他人面临同样的问题 对我来说,问题是(正如
ConnectionClosedException
明确指出的那样)在读取响应之前关闭连接。大致如下:
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();
}
您使用的是哪个版本的rest assured?我使用的是旧版本的Restasured,现在使用的是2.4.1版本&它似乎可以工作。谢谢@fiddler指出这一点。