/*** httpcomponents-client-4.3.6 *@authory*/ Public classHttputil { Public StaticString HttpPost (list<namevaluepair> formparams,FinalString URL) {urlencodedformentity entity=Newurlencodedformentity (Formparams, consts.utf_8); //Setting Network TimeoutsRequestconfig config =Requestconfig.custom (). Setconnectionrequesttimeout (3*1000). Setconnecttimeout (3*1000). SetSocketTimeout (3*1000). build (); HttpPost HttpPost=Newhttppost (URL); Httppost.setconfig (config); Httppost.setentity (entity); Closeablehttpclient httpclient=Httpclients.createdefault (); Closeablehttpresponse Response=NULL; String reuslt= ""; Try{Response=Httpclient.execute (HttpPost); if(Response.getstatusline (). Getstatuscode () = = 200) {httpentity entitycontent=response.getentity (); if(Entitycontent! =NULL) {reuslt= Entityutils.tostring (entitycontent, consts.utf_8);//specify encoding format to prevent Chinese garbled characters } }Else{Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, "Network link timed out"); } } Catch(IOException ex) {Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, ex); }finally { Try{ if(response!=NULL) {response.close (); } }Catch(IOException ex) {Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, ex); } } returnreuslt; } }
Simple use of httpcomponents-client-4.3.6 HttpPost