当我使用CloseableHttpClient和excute方法时,第一次工作正常,但之后就再也没有成功过。它将抛出异常,说明“连接池已关闭”
有人这么说是因为我还没有关闭客户端有人说这是httpclient4.3中的错误
我的项目不存在上面的问题,但它仍然不会工作
CloseableHttpClient httpClient = HttpClientManagerUtils.getHttpClient();
try {
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
log.info("调用网易接口成功,url:{},param:{}", url, JSONObject.toJSON(param));
HttpEntity entity = response.getEntity();
if (entity.getContentType().toString().contains("application/json")) {
String responseString = EntityUtils.toString(entity, "utf-8");
return JSON.toJavaObject(JSON.parseObject(responseString), NimResult.class);
}
} else {
throw new RuntimeException("http code : " + response.getStatusLine().getStatusCode() + ", exception:" +
EntityUtils.toString(response.getEntity()));
}
} catch (Exception e) {
log.error("调用网易接口异常,url:{},param:{}", url, JSONObject.toJSON(param), e);
} finally {
httpClient.close();
}
发布于 2017-09-07 14:06:13
我不应该关闭客户端,而应该关闭响应。客户端将自动关闭,因为httpclient 4.4我已经解决了这个问题:)
https://stackoverflow.com/questions/46088669
复制相似问题