HttpClient send a POST request, GET request

Source: Internet
Author: User

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

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.