/*** 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