HttpClient tool class, httpclient
Package com. chinatelecom. personalcustom. common. util; import com. chinatelecom. personalcustom. personalCustomConst; import com. chinatelecom. personalcustom. model. wapLogBean; import com. fasterxml. jackson. databind. objectMapper; import com. ning. http. client. asyncHttpClient; import com. ning. http. client. asyncHttpClient. boundRequestBuilder; import com. ning. http. client. asyncHttpClientConfig; import com. ning. http. clie Nt. providers. netty. nettyResponse; import org. apache. commons. httpclient. *; import org. apache. commons. httpclient. methods. getMethod; import org. apache. commons. httpclient. methods. postMethod; import org. apache. commons. httpclient. methods. stringRequestEntity; import org. apache. commons. httpclient. params. httpConnectionManagerParams; import org. apache. commons. httpclient. params. httpMethodParams; import org. jboss. ne Tty. handler. codec. http. httpHeaders; import java.net. URLEncoder; import java. util. map; import java. util. map. entry; import java. util. set; public class HttpUtil {/*** request timeout time */private static final int SO_TIMEOUT = 45000; /*** Number of Retries upon request failure */private static final int RETRY_COUNT = 5;/*** maximum number of allowed connections */private static final int MAX_TOTAL_CONNECTION = 200; /*** asynchronous request object */private static AsyncHttpClient asyncHttpClien T; private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager (); static {HttpConnectionManagerParams params = new HttpConnectionManagerParams (); params. setMaxTotalConnections (MAX_TOTAL_CONNECTION); connectionManager. setParams (params);}/*** sets the asynchronous request parameter */static {AsyncHttpClientConfig. builder builder = new AsyncHttpClientConfig. builder (); builder. setMaximum ConnectionsTotal (300); builder. setMaximumConnectionsPerHost (90); builder. setRequestTimeoutInMs (SO_TIMEOUT); asyncHttpClient = new AsyncHttpClient (builder. build ();}/*** get synchronization request connection */private static HttpClient getHttpClientInstance () {HttpClient client = new HttpClient (connectionManager); client. getParams (). setParameter (HttpMethodParams. HTTP_CONTENT_CHARSET, "UTF-8"); client. getParams (). setParameter (Http MethodParams. RETRY_HANDLER, new DefaultHttpMethodRetryHandler (RETRY_COUNT, true); client. getParams (). setSoTimeout (SO_TIMEOUT); return client;}/*** POST Request */public static HttpResponse post (String url, Map <String, Object> params) throws Exception {HttpResponse response = new HttpResponse (); HttpClient client = getHttpClientInstance (); PostMethod postMethod = new PostMethod (url); postMethod. addRequestHea Der (HttpHeaders. names. USER_AGENT, PersonalCustomConst. USER_AGENT); postMethod. setRequestHeader (HttpHeaders. names. CONTENT_TYPE, "UTF-8"); ObjectMapper mapper = new ObjectMapper (); try {StringRequestEntity stringRequestEntity = new StringRequestEntity (mapper. writeValueAsString (params); postMethod. setRequestEntity (stringRequestEntity); int returnCode = client.exe cuteMethod (postMethod); response. setStatusC Ode (returnCode); response. setBody (postMethod. getResponseBodyAsString ();} finally {postMethod. releaseConnection ();} return response;}/*** POST Request */public static HttpResponse post (String url, String params) throws Exception {HttpResponse response = new HttpResponse (); httpClient client = getHttpClientInstance (); PostMethod postMethod = new PostMethod (url); postMethod. addRequestHeader (HttpHeaders. names. USER_AGENT, PersonalCustomConst. USER_AGENT); postMethod. setRequestHeader (HttpHeaders. names. CONTENT_TYPE, "UTF-8"); try {StringRequestEntity stringRequestEntity = new StringRequestEntity (params); postMethod. setRequestEntity (stringRequestEntity); int returnCode = client.exe cuteMethod (postMethod); response. setStatusCode (returnCode); response. setBody (postMethod. getResponseBodyAsString ();} finally {postMeth Od. releaseConnection ();} return response;}/*** GET request */public static HttpResponse get (String url, Map <String, String> params) throws Exception {HttpResponse response = new HttpResponse (); HttpClient client = getHttpClientInstance (); GetMethod getMethod = null; try {StringBuffer buffer = new StringBuffer (); buffer. append (url ). append ("? T = "). append (System. currentTimeMillis (); Set <Entry <String, String> entries = params. entrySet (); for (Entry <String, String> entry: entries) {if (entry! = Null & entry. getValue ()! = Null) {buffer. append ("&"). append (entry. getKey ()). append ("= "). append (URLEncoder. encode (entry. getValue (), "UTF-8");} getMethod = new GetMethod (buffer. toString (); getMethod. setRequestHeader (HttpHeaders. names. CONTENT_TYPE, "UTF-8"); getMethod. getParams (). setContentCharset ("UTF-8"); int returnCode = client.exe cuteMethod (getMethod); response. setStatusCode (returnCode); response. setBody (getMethod. getR EsponseBodyAsString ();} finally {if (getMethod! = Null) {getMethod. releaseConnection () ;}} return response;}/*** asynchronous get request */public static HttpResponse aysnGet (String url, Map <String, Object> params, WapLogBean wapLogBean) throws Exception {HttpResponse response = new HttpResponse (); try {StringBuffer buffer = new StringBuffer (); buffer. append (url); if (params! = Null & params. size ()> 0) {buffer. append ("? T = "). append (System. currentTimeMillis (); Set <Entry <String, Object> entries = params. entrySet (); for (Entry <String, Object> entry: entries) {if (entry! = Null & entry. getValue ()! = Null) {buffer. append ("&"). append (entry. getKey ()). append ("= "). append (entry. getValue () ;}} wapLogBean. setServerUrl (buffer. toString (); BoundRequestBuilder builder = asyncHttpClient. prepareGet (buffer. toString (); builder = builder. addHeader (HttpHeaders. names. USER_AGENT, PersonalCustomConst. USER_AGENT); NettyResponse nettyResponse = (NettyResponse) builder.exe cute (). get (); response. setStatusCode (net TyResponse. getStatusCode (); response. setBody (nettyResponse. getResponseBody ();} catch (Exception e) {throw e;} return response;}/*** asynchronous post Request */public static HttpResponse aysnPost (String url, map <String, Object> params, String userAgent) throws Exception {HttpResponse response = new HttpResponse (); try {BoundRequestBuilder builder = asyncHttpClient. preparePost (url); builder = builder. addHeader (HttpH Eaders. Names. USER_AGENT, userAgent); if (params! = Null & params. size ()> 0) {builder = builder. setBody (JsonUtil. entityToJson (params);} NettyResponse nettyResponse = (NettyResponse) builder.exe cute (). get (); response. setStatusCode (nettyResponse. getStatusCode (); response. setBody (nettyResponse. getResponseBody ();} catch (Exception e) {throw e;} return response;}/*** send HTTP Request */@ SuppressWarnings ("deprecation ") public static String post (String url, St Ring body, String userAgent, String contentType) throws Exception {String result = null; HttpClient client = getHttpClientInstance (); PostMethod method = new PostMethod (url); method. addRequestHeader ("User-Agent", userAgent); method. setRequestHeader ("Content-Type", contentType); method. setRequestBody (body); try using client.exe cuteMethod (method); result = method. getResponseBodyAsString ();} finally {method. rele AseConnection ();} return result;} public static void destroy () {if (asyncHttpClient! = Null) {asyncHttpClient. close ();}}}