HttpClient GET Request (POST) Request

Source: Internet
Author: User
Tags http post

One. Get request with no parameters

Create a HttpClient object
Closeablehttpclient httpclient = Httpclients.createdefault ();

Create an HTTP GET request
HttpGet httpget = new HttpGet ("http://www.baidu.com/");

Closeablehttpresponse response = null;
try {
Execute request
Response = Httpclient.execute (HttpGet);
Determine if the return status is 200
if (Response.getstatusline (). Getstatuscode () = = 200) {
String content = entityutils.tostring (response.getentity (), "UTF-8");
System.out.println ("Content Length:" +content.length ());
}
} finally {
if (response! = NULL) {
Response.close ();
}
Httpclient.close ();
}

Two. Get request with parameters

Create a HttpClient object
Closeablehttpclient httpclient = Httpclients.createdefault ();

Defining parameters for requests
Uri uri = new UriBuilder ("http://www.baidu.com/s"). Setparameter ("WD", "Java"). Build ();

System.out.println (URI);

Create an HTTP GET request
HttpGet httpget = new HttpGet (URI);

Closeablehttpresponse response = null;
try {
Execute request
Response = Httpclient.execute (HttpGet);
Determine if the return status is 200
if (Response.getstatusline (). Getstatuscode () = = 200) {
String content = entityutils.tostring (response.getentity (), "UTF-8");
SYSTEM.OUT.PRINTLN (content);
}
} finally {
if (response! = NULL) {
Response.close ();
}
Httpclient.close ();
}

Three. Post request with no parameters

Create a HttpClient object
Closeablehttpclient httpclient = Httpclients.createdefault ();

Create an HTTP POST request
HttpPost HttpPost = new HttpPost ("http://www.oschina.net/");
Set the request header information, can be disguised as browser access
Httppost.setheader ("User-agent", "mozilla/5.0" (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/68.0.3440.106 safari/537.36 ");

Closeablehttpresponse response = null;
try {
Execute request
Response = Httpclient.execute (HttpPost);
Determine if the return status is 200
if (Response.getstatusline (). Getstatuscode () = = 200) {
String content = entityutils.tostring (response.getentity (), "UTF-8");
SYSTEM.OUT.PRINTLN (content);
}
} finally {
if (response! = NULL) {
Response.close ();
}
Httpclient.close ();
}

Four. Post request with parameters

Create a HttpClient object
Closeablehttpclient httpclient = Httpclients.createdefault ();

Create an HTTP POST request
HttpPost HttpPost = new HttpPost ("Http://www.oschina.net/search");

Set 2 post parameters, one is scope, one is Q
list<namevaluepair> parameters = new Arraylist<namevaluepair> (0);
Parameters.Add (New Basicnamevaluepair ("Scope", "project"));
Parameters.Add (New Basicnamevaluepair ("Q", "Java"));
Construct a form-form entity
Urlencodedformentity formentity = new urlencodedformentity (parameters);
Set the request entity to the HttpPost object
Httppost.setentity (formentity);

Closeablehttpresponse response = null;
try {
Execute request
Response = Httpclient.execute (HttpPost);
Determine if the return status is 200
if (Response.getstatusline (). Getstatuscode () = = 200) {
String content = entityutils.tostring (response.getentity (), "UTF-8");
SYSTEM.OUT.PRINTLN (content);
}
} finally {
if (response! = NULL) {
Response.close ();
}
Httpclient.close ();
}

Five. If you consider performance issues, you can create a connection pool as you would create a database connection pool, as in the following example:

Poolinghttpclientconnectionmanager cm = new Poolinghttpclientconnectionmanager ();
Set maximum number of connections
Cm.setmaxtotal (200);
Set the number of concurrency per host address
Cm.setdefaultmaxperroute (20);

Doget (CM);

Doget (CM);

Here is the call

public static void Doget (Httpclientconnectionmanager cm) throws Exception {
Closeablehttpclient httpClient = Httpclients.custom (). Setconnectionmanager (CM). Build ();

Create an HTTP GET request
HttpGet httpget = new HttpGet ("http://www.baidu.com/");

Closeablehttpresponse response = null;
try {
Execute request
Response = Httpclient.execute (HttpGet);
Determine if the return status is 200
if (Response.getstatusline (). Getstatuscode () = = 200) {
String content = entityutils.tostring (response.getentity (), "UTF-8");
System.out.println ("Content length:" + content.length ());
}
} finally {
if (response! = NULL) {
Response.close ();
}
HttpClient cannot be closed here, and if HttpClient is turned off, the connection pool will also be destroyed
Httpclient.close ();
}
}

HttpClient GET Request (POST) 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.