Java Network Request Tool Class (dependency: org.apache.http; Note: HttpClient 4.4,httpcore 4.4)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.