You can download the dependency package here: http://hc.apache.org/downloads.cgi
1 Importjava.util.List;2 3 ImportOrg.apache.http.HttpStatus;4 ImportOrg.apache.http.NameValuePair;5 ImportOrg.apache.http.client.config.RequestConfig;6 Importorg.apache.http.client.entity.UrlEncodedFormEntity;7 ImportOrg.apache.http.client.methods.CloseableHttpResponse;8 ImportOrg.apache.http.client.methods.HttpGet;9 ImportOrg.apache.http.client.methods.HttpPost;Ten Importorg.apache.http.impl.client.CloseableHttpClient; One Importorg.apache.http.impl.client.HttpClients; A Importorg.apache.http.util.EntityUtils; - - /** the * Httpservletutil - * - * @authorYSJ - * @Date: 2015-1-30 pm 2:07:55 + */ - Public classHttpservletutil { + Private Staticcloseablehttpclient httpclient; A Private StaticRequestconfig requestconfig = Requestconfig.custom (). SetSocketTimeout (). Setconnecttimeout (5000). Build (); at - /** - * Post: Access the database and return the data string - * - * @paramparams - * Parameters passed to the server side in * @paramURL - * @returnString Data Strings to * @throwsException + */ - Public StaticString DoPost (list<namevaluepair> params, string url)throwsException { theString result =NULL; *HttpClient =Httpclients.createdefault (); $HttpPost HttpPost =Newhttppost (URL);Panax NotoginsengHttppost.setentity (Newurlencodedformentity (params)); - //set the request and transmit timeout times the Httppost.setconfig (requestconfig); +Closeablehttpresponse Httpresp =Httpclient.execute (httppost); A Try { the intStatusCode =httpresp.getstatusline (). Getstatuscode (); + //judgment is enough to ask for success - if(StatusCode = =HTTPSTATUS.SC_OK) { $System.out.println ("Status code:" +statusCode); $SYSTEM.OUT.PRINTLN ("Request succeeded!")); - //get the returned data -result = Entityutils.tostring (Httpresp.getentity (), "UTF-8"); the}Else { -System.out.println ("Status code:"Wuyi+httpresp.getstatusline (). Getstatuscode ()); theSYSTEM.OUT.PRINTLN ("HttpPost mode request failed!")); - } Wu}finally { - httpresp.close (); About httpclient.close (); $ } - returnresult; - } - A /** + * Get: Access the database and return the data string the * - * @paramURL $ * @returnString Data Strings the * @throwsException the */ the Public StaticString doget (string url)throwsexception{ theString result =NULL; -HttpClient =Httpclients.createdefault (); inHttpGet HttpGet =Newhttpget (URL); the //set the request and transmit timeout times the Httpget.setconfig (requestconfig); AboutCloseablehttpresponse Httpresp =Httpclient.execute (httpget); the Try { the intStatusCode =httpresp.getstatusline (). Getstatuscode (); the //judgment is enough to ask for success + if(StatusCode = =HTTPSTATUS.SC_OK) { -System.out.println ("Status code:" +statusCode); theSYSTEM.OUT.PRINTLN ("Request succeeded!"));Bayi //get the returned data theresult = Entityutils.tostring (Httpresp.getentity (), "UTF-8"); the}Else { -System.out.println ("Status code:" -+httpresp.getstatusline (). Getstatuscode ()); theSYSTEM.OUT.PRINTLN ("HttpGet mode request failed!")); the } the}finally { the httpresp.close (); - httpclient.close (); the } the returnresult; the }94}
Java Network Request Tool Class (dependency: org.apache.http; Note: HttpClient 4.4,httpcore 4.4)