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.client.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.netty.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 time-out */private static final int So_tiMeout = 45000;/** * Retry count when request fails */private static final int retry_count = 5;/** * Maximum allowable number of connections */private static final int Max_tota L_connection = 200;/** * Asynchronous Request object */private static asynchttpclient asynchttpclient;private static Multithreadedhttpconnectionmanager ConnectionManager = new Multithreadedhttpconnectionmanager (); static { Httpconnectionmanagerparams params = new Httpconnectionmanagerparams ();p arams.setmaxtotalconnections (MAX_TOTAL_ CONNECTION); Connectionmanager.setparams (params);} /** * Set Asynchronous request parameters */static {Asynchttpclientconfig.builder Builder = new Asynchttpclientconfig.builder (); Builder.setmaximumconnectionstotal (+); Builder.setmaximumconnectionsperhost (90); Builder.setrequesttimeoutinms (so_timeout); asynchttpclient = new Asynchttpclient (Builder.build ());} /** * Get sync request connection */private static HttpClient gethttpclientinstance () {HttpClient client = new HttpClient (ConnectionManager) ; Client.getparams (). Setparameter (Httpmethodparams.http_content_charset, "UTF-8"); Client.getparams (). SETparameter (httpmethodparams.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);p ostmethod.addrequestheader (HttpHeaders.Names.USER_AGENT, personalcustomconst.user_agent);p Ostmethod.setrequestheader (HttpHeaders.Names.CONTENT_TYPE, "UTF-8"); O Bjectmapper mapper = new Objectmapper (); try {stringrequestentity stringrequestentity = new Stringrequestentity ( Mapper.writevalueasstring (params));p ostmethod.setrequestentity (stringrequestentity); int ReturnCode = Client.executemethod (Postmethod); Response.setstatuscode (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);p ostmethod.addrequestheader (HttpHeaders.Names.USER_AGENT, personalcustomconst.user_agent);p Ostmethod.setrequestheader (HttpHeaders.Names.CONTENT_TYPE, "UTF-8"); try { Stringrequestentity stringrequestentity = new Stringrequestentity (params);p ostmethod.setrequestentity ( stringrequestentity); int returncode = Client.executemethod (Postmethod); Response.setstatuscode (ReturnCode); Response.setbody (Postmethod.getresponsebodyasstring ());} finally {postmethod.releaseconnection ();} return response;} /** * GET request */public static HttpResponse get (String URL, map<string,string> params) throws Exception {HttpResponse R Esponse = 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.executemethod (GetMethod); Response.setstatuscode (ReturnCode); Response.setbody ( Getmethod.getresponsebodyasstring ());} finally {if (GetMethod! = null) {getmethod.releaseconnection ();}} return response;} /** * Asynchronous GET request */public static HttpResponse Aysnget (String url,map<string, object> params,waplogbean Waplogbean) thr OWS 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.execute (). get (); Response.setstatuscode ( Nettyresponse.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.preparepos T (URL); Builder = Builder.addheader (httpheaders.names.user_agent,useragent); if (params! = null && params.size () > 0) {builder = Builder.setbody (Jsonutil.entitytojson (params)); } nettyresponse Nettyresponse = (nettyresponse) builder.execute (). 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,string body, string useragent, Stri Ng 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 {Client.executemethod (method); ResUlt = Method.getresponsebodyasstring ();} finally {method.releaseconnection ();} return result;} public static void Destroy () {if (asynchttpclient! = null) {asynchttpclient.close (); }}}
HttpClient Tool Class