httpResponse is always null
java只有按值传递参数的,你在doGet中先初始化为null, 然后在getHttpClientResult中赋值,最后在doGet中调用release,其实按java的传值规则,你doGet中的这个httpResponse永远都是null的。其他几个接口也是类似的问题,请查证!
@zhouciming 提到的问题确实存在,既然已经把CloseableHttpResponse的内容封装到HttpClientResult返回了,返回后也是直接关闭了,不如直接在getHttpClientResult中关闭。
/** * Description: 获得响应结果 */ public static HttpClientResult getHttpClientResult(CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws IOException { // 执行请求 try (CloseableHttpResponse httpResponse = httpClient.execute(httpMethod)) { // 获取返回结果 if (httpResponse != null && httpResponse.getStatusLine() != null) { String content = ""; if (httpResponse.getEntity() != null) { content = EntityUtils.toString(httpResponse.getEntity(), ENCODING); } return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content); } return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR); } finally { if (httpClient != null) { httpClient.close(); } } }