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