} catch (Exception e) {
LOGGER.error("HttpClientHelper parseResponse error", e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 如果httpEntity没有被完全消耗,那么连接无法安全重复使用,将被关闭并丢弃
try {
EntityUtils.consume(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
private String reponseHandle(CloseableHttpResponse response) {
String result = "";
// 获取响应体
HttpEntity httpEntity = null;
try {
// 获取响应状态
int statusCode = response.getStatusLine().getStatusCode();
// 没有正常响应
if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
throw new RuntimeException("statusCode : " + statusCode);
}
// 获取响应体
httpEntity = response.getEntity();
if (httpEntity !=null) {
result = EntityUtils.toString(httpEntity);
}
} catch (Exception e) {
LOGGER.error("HttpClientHelper reponseHandle error", e);
} finally {
// 如果httpEntity没有被完全消耗,那么连接无法安全重复使用,将被关闭并丢弃
try {
EntityUtils.consume(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
第四步:创建HttpClient无效连接清除类
/**
* 定期清理无效的http连接
* @author viruser
*
*/
package com.123.lenovo.adapter.se.common;
import java.util.concurrent.TimeUnit;
import javax.annotation.PreDestroy;
import org.apache.http.conn.HttpClientConnectionManager;
/**
* 定期清理无效的http连接
* @author viruser
*
*/
public class IdleConnectionEvictor extends Thread {