<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
private final static int SOCKET_TIMEOUT = 5000;
private final static int CONNECTION_TIMEOUT = 30000;
private final static int MAX_CONN_PRE_HOST = 2;
private final static int MAX_CONN = 2;
private static HttpConnectionManager getHttpConnectionManager() {
HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = httpConnectionManager.getParams();
params.setConnectionTimeout(CONNECTION_TIMEOUT);
params.setSoTimeout(SOCKET_TIMEOUT);
params.setDefaultMaxConnectionsPerHost(MAX_CONN_PRE_HOST);
params.setMaxTotalConnections(MAX_CONN);
return httpConnectionManager;
}
public static HttpResult get(String url) {
HttpResult result = new HttpResult();
HttpClient httpClient = new HttpClient(getHttpConnectionManager());
logger.info("请求路径:{}", url);
GetMethod getMethod = new GetMethod(url);
try {
getMethod.addRequestHeader("Content-Type", "text/html; charset=UTF-8");
getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
int httpState = httpClient.executeMethod(getMethod);
handleRespInfo(result, getMethod.getResponseBody(), getMethod.getResponseCharSet(), httpState, getMethod.getStatusLine());
} catch (Exception e) {
result.setCode(500);
result.setMessage(e.getMessage());
logger.error("请求异常:", e);
} finally {
getMethod.releaseConnection();
}
return result;
}
public static HttpResult post(String url, String jsonParam) {
HttpResult result = new HttpResult();
HttpClient httpClient = new HttpClient(getHttpConnectionManager());
logger.info("请求路径:{}", url);
PostMethod postMethod = new PostMethod(url);
try {
postMethod.setRequestHeader("Content-Type", "application/json;charset=utf-8");
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
if (StringUtils.isNotBlank(jsonParam)) {
logger.info("请求报文:{}", jsonParam);
postMethod.setRequestEntity(new StringRequestEntity(jsonParam, "application/json", "utf-8"));
}
int httpState = httpClient.executeMethod(postMethod);
handleRespInfo(result, postMethod.getResponseBody(), postMethod.getResponseCharSet(), httpState, postMethod.getStatusLine());
} catch (Exception e) {
result.setCode(500);
result.setMessage(e.getMessage());
logger.error("请求异常:", e);
} finally {
postMethod.releaseConnection();
}
return result;
}
private static void handleRespInfo(HttpResult result, byte[] responseBody, String responseCharSet,
int statusCode, StatusLine statusLine) throws IOException {
String respInfo = new String(responseBody, responseCharSet);
logger.info("响应报文:{}", respInfo);
result.setCode(statusCode);
if (statusCode == HttpStatus.SC_OK) {
result.setData(respInfo);
logger.info("请求成功");
} else {
result.setMessage(statusLine.toString());
logger.info("请求失败:{}", statusLine.toString());
}
}
}