Java tutorial
/** * @(#)AccHttpClient.java, Mar 29, 2014. * * Copyright 2014 Yodao, Inc. All rights reserved. * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package outfox.dict.contest.util; import java.util.concurrent.TimeUnit; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.params.ClientPNames; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.AutoRetryHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; /** * * HttpClient??http * @author wangzhen * */ public class HttpToolKit { private static final Logger LOG = Logger.getLogger(HttpToolKit.class); public enum Method { GET, POST } HttpClient client = null; private PoolingClientConnectionManager connectionManager; public HttpClient getClient() { return client; } public void setClient(HttpClient client) { this.client = client; } private HttpToolKit() { this(10, 100, 10000, 10000, CookiePolicy.IGNORE_COOKIES, true, true); } private static class SSLClientHolder { private static final HttpToolKit instance = new HttpToolKit(); } public static HttpToolKit getInstance() { return SSLClientHolder.instance; } /** * @param maxConnectPerHost * @param maxConnection * @param connectTimeOut * @param socketTimeOut * @param cookiePolicy * @param isAutoRetry * @param redirect */ public HttpToolKit(int maxConnectPerHost, int maxConnection, int connectTimeOut, int socketTimeOut, String cookiePolicy, boolean isAutoRetry, boolean redirect) { Scheme https = new Scheme("https", 443, SSLSocketFactory.getSocketFactory()); Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory()); SchemeRegistry sr = new SchemeRegistry(); sr.register(https); sr.register(http); connectionManager = new PoolingClientConnectionManager(sr, socketTimeOut, TimeUnit.MILLISECONDS); connectionManager.setDefaultMaxPerRoute(maxConnectPerHost); connectionManager.setMaxTotal(maxConnection); HttpParams params = new BasicHttpParams(); params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, connectTimeOut); params.setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy); params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, redirect); params.setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false); if (isAutoRetry) { client = new AutoRetryHttpClient(new DefaultHttpClient(connectionManager, params)); } else { client = new DefaultHttpClient(connectionManager, params); } } /** * If you call doGet , you should call closeQuiet() to close the response. * @param url * @param headers * @param urlParameters * @return * @throws Exception */ public HttpResponse doGet(String composeQueryUrl) { try { HttpGet get = new HttpGet(composeQueryUrl); HttpResponse resp = client.execute(get); return resp; } catch (Exception e) { LOG.error("client execute exception, ", e); } return null; } public HttpResponse doPost(String composeQueryUrl, HttpEntity entity) { try { HttpPost post = new HttpPost(composeQueryUrl); post.setEntity(entity); return client.execute(post); } catch (Exception e) { LOG.error("client execute exception, ", e); } return null; } public static void closeQuiet(HttpResponse response) { if (response != null) { EntityUtils.consumeQuietly(response.getEntity()); } } }