HttpClient Interface Call

Source: Internet
Author: User
Tags getmessage

String url = "Http://127.0.0.1:8080/api"; //then get company information based on the table nameHttpPost HttpPost =Newhttppost (URL); List<NameValuePair> params =NewArraylist<namevaluepair>(); Params.add (NewBasicnamevaluepair ("Comname", Comname)); HttpResponse HttpResponse=NULL; Httpentity httpentity=NULL; Try {
Set the timeout time httpclient4.5 to build the Requestconfig object Requestconfig requestconfig=Requestconfig.custom (). Setconnecttimeout ((Setconnectionrequesttimeout). (1000). SetSocketTimeout (5000). build ();  Httppost.setconfig (Requestconfig); Httppost.setentity (Newurlencodedformentity (params, HTTP. Utf_8)); HttpClient HttpClient=Newdefaulthttpclient (); HttpResponse=Httpclient.execute (HttpPost); Httpentity=httpresponse.getentity (); } Catch(unsupportedencodingexception E1) {Logger.error (E1.getmessage ()); } Catch(clientprotocolexception E1) {Logger.error (E1.getmessage ()); } Catch(IOException E1) {Logger.error (E1.getmessage ()); } if(Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Request OK Try{String result=entityutils.tostring (httpentity); Jsonobject Data=Jsonobject.fromobject (httpentity); if("$". Equals (Data.get ("code"))) {System.out.println ("Call succeeded"); }Else{logger.error (Data.get ("Message")); } } Catch(Exception e) {logger.error (E.getmessage ()); } }Else { Try{logger.error (entityutils.tostring (httpentity)); } Catch(ParseException e) {logger.error (E.getmessage ()); } Catch(IOException e) {logger.error (E.getmessage ()); } }

HttpClient Interface Call

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.