带连接池的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;
    } 


 

你可能感兴趣的:(httpclient)