[color=brown][size=x-large]1,首先创建httpclient的单例对象[/size][/color]
public class HttpClientFactory {
private static HttpClient httpClient;
/** 设置对单host最大的连接数(默认所有链接都对同一个host) */
private static int maxConnPerHost = 32;
/** 设置最大链接数 默认为20个 */
private static int maxTotalConn = 128;
/** 链接url的超时时间,单位是毫秒(不是response超时时间) */
private static int connectionTimeOut = 12000;
/** 等待响应超时时间 */
private static int soTimeOut = 10000;
/** 等待连接池超时时间 */
private static int connectionPoolTimeOut = 10000;
/**
* 单例模式,杜绝手动实例化
*/
private HttpClientFactory() {
}
/**
* httpclient的单例
*
* @return
*/
public static HttpClient getHttpClient() {
if (httpClient == null) {
synchronized (HttpClientFactory.class) {
if (httpClient == null) {
createHttpClient();
}
}
}
return httpClient;
}
/**
* HttpClient的创建方法
*/
private static void createHttpClient() {
final HttpConnectionManager httpConManager = populateMultiThreadConnectionMananger();
retryHandler(httpConManager);
}
/**
* 这里组装的是 MultiThreadedHttpConnectionManager
*
* @return MultiThreadedHttpConnectionManager
*/
private static HttpConnectionManager populateMultiThreadConnectionMananger() {
final HttpConnectionManager httpConManager = new MultiThreadedHttpConnectionManager();
final HttpConnectionManagerParams conManagerParam = new HttpConnectionManagerParams();
// 设置超时时间等参数
conManagerParam.setMaxTotalConnections(maxTotalConn);
conManagerParam.setDefaultMaxConnectionsPerHost(maxConnPerHost);
conManagerParam.setConnectionTimeout(connectionTimeOut);
conManagerParam.setSoTimeout(soTimeOut);
httpConManager.setParams(conManagerParam);
return httpConManager;
}
/**
* 组装请求重试handler,将默认重试3次改为不重试
*
* @param conManager
* @param retryhandler
*/
private static void retryHandler(
final HttpConnectionManager conManager) {
// 将默认重试设为0
//如果设置为0, 则代表不会自动重试,默认连接超时时间1s钟,
//经测试10, 则会重试10次连接每次1s钟,所以会耗时11s
//true: 是否将方法重试 , false:只重试url连接
final DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(
10, false);
httpClient = new HttpClient(conManager);
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
retryhandler);
httpClient.getParams().setConnectionManagerTimeout(connectionPoolTimeOut);
}
}
[color=brown][size=x-large]2,创建postMethod的对象,组装method的参数[/size][/color]
public class PostMethodFactory {
/** content_type名称常量 */
private final static String CONTENT_TYPE_KEY = "Content-Type";
/** content_type值常量 */
private final static String CONTENT_TYPE_VALUE = "text/xml; charset=";
/**
* 创建PostMethod
* @param url 目标url
* @param soTimeOut response超时时间
* @param content 发送内容
* @return PostMethod
*/
public static PostMethod createPostMethod(final String url, final int soTimeOut,
final String content, String requestCharset,
String responseCharset) {
//validate first
if (StringUtils.isBlank(url) || StringUtils.isBlank(content)) {
System.out.println("参数检查失败");
}
final PostMethod postMethod = new PostMethod(url);
postMethod.addRequestHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_VALUE + requestCharset);
//对method设置超时时间
final HttpMethodParams httpMethodParams = new HttpMethodParams();
httpMethodParams.setSoTimeout(soTimeOut);
httpMethodParams.setContentCharset(responseCharset);
postMethod.setParams(httpMethodParams);
try {
postMethod.setRequestEntity(new StringRequestEntity(content, null, requestCharset));
} catch (UnsupportedEncodingException e) {
System.out.println("不支持字符集");
}
return postMethod;
}
}
[color=brown][size=x-large]3,测试请求响应数据[/size][/color]
// 获取httpclient连接实例
HttpClient httpClient = HttpClientFactory.getHttpClient();
// http的prototype 协议类型
PostMethod postMethod = PostMethodFactory.createPostMethod(url,
soTimeOut, content, requestCharset, responseCharset);
int returnCode = httpClient.executeMethod(postMethod);
public class HttpClientFactory {
private static HttpClient httpClient;
/** 设置对单host最大的连接数(默认所有链接都对同一个host) */
private static int maxConnPerHost = 32;
/** 设置最大链接数 默认为20个 */
private static int maxTotalConn = 128;
/** 链接url的超时时间,单位是毫秒(不是response超时时间) */
private static int connectionTimeOut = 12000;
/** 等待响应超时时间 */
private static int soTimeOut = 10000;
/** 等待连接池超时时间 */
private static int connectionPoolTimeOut = 10000;
/**
* 单例模式,杜绝手动实例化
*/
private HttpClientFactory() {
}
/**
* httpclient的单例
*
* @return
*/
public static HttpClient getHttpClient() {
if (httpClient == null) {
synchronized (HttpClientFactory.class) {
if (httpClient == null) {
createHttpClient();
}
}
}
return httpClient;
}
/**
* HttpClient的创建方法
*/
private static void createHttpClient() {
final HttpConnectionManager httpConManager = populateMultiThreadConnectionMananger();
retryHandler(httpConManager);
}
/**
* 这里组装的是 MultiThreadedHttpConnectionManager
*
* @return MultiThreadedHttpConnectionManager
*/
private static HttpConnectionManager populateMultiThreadConnectionMananger() {
final HttpConnectionManager httpConManager = new MultiThreadedHttpConnectionManager();
final HttpConnectionManagerParams conManagerParam = new HttpConnectionManagerParams();
// 设置超时时间等参数
conManagerParam.setMaxTotalConnections(maxTotalConn);
conManagerParam.setDefaultMaxConnectionsPerHost(maxConnPerHost);
conManagerParam.setConnectionTimeout(connectionTimeOut);
conManagerParam.setSoTimeout(soTimeOut);
httpConManager.setParams(conManagerParam);
return httpConManager;
}
/**
* 组装请求重试handler,将默认重试3次改为不重试
*
* @param conManager
* @param retryhandler
*/
private static void retryHandler(
final HttpConnectionManager conManager) {
// 将默认重试设为0
//如果设置为0, 则代表不会自动重试,默认连接超时时间1s钟,
//经测试10, 则会重试10次连接每次1s钟,所以会耗时11s
//true: 是否将方法重试 , false:只重试url连接
final DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(
10, false);
httpClient = new HttpClient(conManager);
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
retryhandler);
httpClient.getParams().setConnectionManagerTimeout(connectionPoolTimeOut);
}
}
[color=brown][size=x-large]2,创建postMethod的对象,组装method的参数[/size][/color]
public class PostMethodFactory {
/** content_type名称常量 */
private final static String CONTENT_TYPE_KEY = "Content-Type";
/** content_type值常量 */
private final static String CONTENT_TYPE_VALUE = "text/xml; charset=";
/**
* 创建PostMethod
* @param url 目标url
* @param soTimeOut response超时时间
* @param content 发送内容
* @return PostMethod
*/
public static PostMethod createPostMethod(final String url, final int soTimeOut,
final String content, String requestCharset,
String responseCharset) {
//validate first
if (StringUtils.isBlank(url) || StringUtils.isBlank(content)) {
System.out.println("参数检查失败");
}
final PostMethod postMethod = new PostMethod(url);
postMethod.addRequestHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_VALUE + requestCharset);
//对method设置超时时间
final HttpMethodParams httpMethodParams = new HttpMethodParams();
httpMethodParams.setSoTimeout(soTimeOut);
httpMethodParams.setContentCharset(responseCharset);
postMethod.setParams(httpMethodParams);
try {
postMethod.setRequestEntity(new StringRequestEntity(content, null, requestCharset));
} catch (UnsupportedEncodingException e) {
System.out.println("不支持字符集");
}
return postMethod;
}
}
[color=brown][size=x-large]3,测试请求响应数据[/size][/color]
// 获取httpclient连接实例
HttpClient httpClient = HttpClientFactory.getHttpClient();
// http的prototype 协议类型
PostMethod postMethod = PostMethodFactory.createPostMethod(url,
soTimeOut, content, requestCharset, responseCharset);
int returnCode = httpClient.executeMethod(postMethod);