pal*_*han 6 apache http chunked-encoding rest-assured 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());
Run Code Online (Sandbox Code Playgroud)
关于上一个声明,我收到以下异常:
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
任何人都可以指导我解决此异常,并在缺少任何内容或任何不正确的实现时指出我。
我有一个与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!
Run Code Online (Sandbox Code Playgroud)
修复是显而易见的。安排代码,以便关闭后不使用响应:
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();
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*han -1
也许您可以尝试摆弄连接配置?例如:
given().config(RestAssured.config().connectionConfig(connectionConfig().closeIdleConnectionsAfterEachResponse())). ..
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15102 次 |
| 最近记录: |