HttpClient get (with parameters), Post request mode, Tool class method

Source: Internet
Author: User

/*** Connect/Disconnect Operation Post mode* @paramUrl * @paramJson */Private BooleanConnordisconnoperator (String URL, String JSON) {closeablehttpclient client =NullCloseablehttpresponse response =NullBoolean flag =Falsetry{HttpPost HttpPost =New HttpPost (URL);Requestconfig requestconfig = Requestconfig.Custom (). Setconnecttimeout (50000). SetSocketTimeout (50000). Setconnectionrequesttimeout (120000). Build ();Httppost.setconfig (Requestconfig);list<basicnamevaluepair> list = Lists.Newarraylistwithexpectedsize (1);List.add (New Basicnamevaluepair ("JSON", JSON));Httppost.setentity (New Urlencodedformentity (list));Client = httpclients.Createdefault ();Response = Client.execute (HttpPost);if (Response.getstatusline (). Getstatuscode () = =() {InputStream is = response.getentity (). GetContent ();Map<string,object> m =New Objectmapper (). ReadValue (Stringutil.GetString (IS), Map.Class;String strstate = M.get ("State"). ToString ();If"SUCCESS". Equals (Strstate)) {flag =True} }else{Log.error (This.getclass (),"Connordisconnoperator method fail:" + response.getstatusline (). Getstatuscode ());} }catch (Exception ex) {Log.error (This.getclass (),"Connordisconnoperator Method Error", ex);}finally {if (response! =NULL) {try {response.close ();}catch (IOException ex) {Log.error (This.getclass (), "Close response method Error", ex);} } if (client! = null) { try {client.close (); } catch (IOException ex) { log.error (this.getclass (), "Close Client Method", ex); } }} return flag;}                
Get modePublic optional<map<string,object>>Connordisconnoperator (String Atisairportcode, String URL) {closeablehttpclient client =NullCloseablehttpresponse response =Nulltry{HttpGet HttpGet =New HttpGet (URL +"? airportcode=" +atisairportcode);Requestconfig requestconfig = Requestconfig.Custom (). SetSocketTimeout ((Setconnecttimeout).(Setconnectionrequesttimeout).120000). Build ();Httpget.setconfig (Requestconfig);Client = httpclients.Createdefault ();Response = Client.execute (HttpGet);if (Response.getstatusline (). Getstatuscode () = =200 | | Response.getstatusline (). Getstatuscode () = =) {map<string,object> map = maps.Newhashmapwithexpectedsize (4);InputStream is = Response.getentity (). GetContent ();Jsonnode JN =New Objectmapper (). Readtree (Stringutil.GetString (IS)); Parse return resultString result = Jn.get ("State"). Astext ();if (Result.equals ("SUCCESS")) {System.Out.println ("-----------------------------Success");}else {System.Out.println ("-----------------------------failure");}Return Optional.of (map);}else{Return Optional.Absent ();} }catch (Exception ex) {Log.error (This.getclass (),"Getosinfo () Error", ex);Return Optional.absent () ;} finally {if (response! =  NULL) {try {response.close () ;} catch (IOException ex) {log.error (this.getclass () " close IO error ", ex) ;}} if (client! = null) {try { Client.close (); } catch (IOException ex) {Log.error (This.getclass (), "Close IO error", ex);}} }} 

HttpClient get (with parameters), Post request method, Tool class method

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.