Java CloseableHttpClient连接池关闭
当我使用CloseableHttpClient和do Execute方法时,它在第一次正常工作,但之后从未成功。 它将抛出表示“连接池关闭”的异常 有人这么说是因为我没有关闭客户 有人说这是httpclient 4.3中的一个bug 我的项目不存在上述问题,但仍然无法正常工作Java CloseableHttpClient连接池关闭,java,httpclient,Java,Httpclient,当我使用CloseableHttpClient和do Execute方法时,它在第一次正常工作,但之后从未成功。 它将抛出表示“连接池关闭”的异常 有人这么说是因为我没有关闭客户 有人说这是httpclient 4.3中的一个bug 我的项目不存在上述问题,但仍然无法正常工作 CloseableHttpClient httpClient = HttpClientManagerUtils.getHttpClient(); try { HttpResponse respon
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();
}
我不应该关闭客户端,而是应该关闭响应。自httpclient 4.4起,客户将自动关闭 我已经解决了:)