//to create a default HttpClient instanceCloseablehttpclient httpclient =gethttpclient (); Closeablehttpresponse Response=NULL; //Create HttpPostHttpPost HttpPost =Newhttppost ("URL address");
Httppost.setheader (HTTP. Content_Type,"Application/json; Charset=utf-8 "); Try{stringentity param=Newstringentity (params, contenttype.application_json); Httppost.setentity (param);Response =Httpclient.execute (HttpPost); Httpentity Entity=response.getentity (); if(Entity! =NULL) {InputStream is=entity.getcontent (); } } Catch(Exception e) {log.error ("Sendpostrequest Error:", E); } finally { Try { //Close the connection and release the resourceResponse.close (); Httpclient.close (); } Catch(Exception e) {log.error ("HttpClient Close error:", E); } }
Send a GET request
Closeablehttpclient httpclient =gethttpclient (); Closeablehttpresponse Response=NULL; HttpGet HttpGet=NewHttpGet ("URL address"); Try{Response=Httpclient.execute (HttpGet); Httpentity Entity=response.getentity (); if(Entity! =NULL) {InputStream is=entity.getcontent (); } } Catch(clientprotocolexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally { Try { //Close the connection and release the resourceResponse.close (); Closehttpclient (httpclient); } Catch(Exception e) {log.error ("HttpClient Close error:", E); } }
Please do not mind borrowing the code of a great God only for the convenience of future reference.
How can a scholar's thing be called stealing, that is called stealing!
HttpClient send a POST request, GET request