import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
public class HttpClientUtil {
private HttpClientUtil() {
}
private static HttpClient httpClient = null;
//静态工厂方法
public static HttpClient getInstanceHttpClient() {
if (httpClient == null) {
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
// 创建 HttpClient 的多线程实例
//在创建httpclient对象的时候,默认使用simpleHttpConnectionManager作为连接管理,而它不适合多线程环境。故修正方法是:自己创建httpclient对象,并指定适合多线程环境的连接管理器。
// httpClient = new HttpClient(connectionManager);
// /*
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
params.setConnectionTimeout(1000);
// params.setSoTimeout(2000);
// 最大连接数
params.setMaxTotalConnections(1);
params.setDefaultMaxConnectionsPerHost(1);
params.setStaleCheckingEnabled(true);
connectionManager.setParams(params);
HttpClientParams httpClientParams = new HttpClientParams();
// 设置httpClient的连接超时,对连接管理器设置的连接超时是无用的
httpClientParams.setConnectionManagerTimeout(1000); //等价于4.2.3中的CONN_MANAGER_TIMEOUT
httpClient = new HttpClient(connectionManager);
httpClient.setParams(httpClientParams);
//另外设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可)
// httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
// */
}
return httpClient;
}
}
HttpClientUtil
原创呜哈哈666888 ©著作权
文章标签 httpclient3 文章分类 Java 后端开发
上一篇:增加linux交换空间
下一篇:windows连接数优化
-
HttpClientUtil 实现doget 和 dopost
HttpClientUtil 实现doget 和 dopost
HttpClientUtil 实现dog -
基于SSM的单点登陆02
pom文件 GResult.java GResultUtil.java CookieUtil.java HttpClientUtil.java
apache java json .net xml