带连接池的httpclient的使用小例

jar包取自于httpclient-4.1.1.jar

1》:设置带连接池的HttpClient

public class HttpClientUtils {

private static final Log log = LogFactory.getLog(HttpClientUtils.class);

private static ThreadSafeClientConnManager cm = null;

static {
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
cm = new ThreadSafeClientConnManager(schemeRegistry);
try {
int maxTotal = Integer.valueOf(ResourceUtil.getSystem("httpclient.max_total"));
cm.setMaxTotal(maxTotal);
} catch (NumberFormatException e) {
log.error("Key[httpclient.max_total] Not Found in systemConfig.properties", e);
}
// 每条通道的并发连接数设置(连接池)
try {
int defaultMaxConnection = Integer.valueOf(ResourceUtil.getSystem("httpclient.default_max_connection"));
cm.setDefaultMaxPerRoute(defaultMaxConnection);
} catch (NumberFormatException e) {
log.error("Key[httpclient.default_max_connection] Not Found in systemConfig.properties", e);
}

}

public static HttpClient getHttpClient() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); // 3000ms

return new DefaultHttpClient(cm, params);
}


public static void release() {
if (cm != null) {
cm.shutdown();
}
}
}

2》:对于HttpClientUtils的使用

public final static UserLoginStatus verifyLogon(String token) {
HttpClient httpClient = HttpClientUtils.getHttpClient();
HttpPost httpPost = new HttpPost(INFOPLATFORM_SERVICE_SESSION);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("interface", "verifyLogon"));
nvps.add(new BasicNameValuePair("token", token));
UserLoginStatus result = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
} else {
HttpEntity entity = response.getEntity();
result = JsonConverter.toObj(EntityUtils.toString(entity), UserLoginStatus.class);
}
} catch (ClientProtocolException e) {
log.error("method: UserLoginStatus [ClientProtocolException]", e);
} catch (IOException e) {
log.error("method: UserLoginStatus [IOException] invoke information platform failed", e);
} catch (ParseException e) {
log.error("method: UserLoginStatus [ParseException] invoke information platform failed", e);
} catch (BaseException e) {
log.error("method: UserLoginStatus [BaseException] invoke information platform failed", e);
}
// 调用接口失败
if (null == result) {
result = new UserLoginStatus();
result.setCall_status("2");
result.setCall_message(INFOPLATFORM_ERR_MSG);
}
return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值