Public class Httpclienttest {
- public static void Main (String args[]) {
- //Create Httpclientbuilder
- Httpclientbuilder Httpclientbuilder = Httpclientbuilder.create ();
- //httpclient
- Closeablehttpclient closeablehttpclient = Httpclientbuilder.build ();
- HttpGet httpget = new HttpGet ("http://www.gxnu.edu.cn/default.html");
- System.out.println (Httpget.getrequestline ());
- try {
- //Execute GET request
- HttpResponse HttpResponse = Closeablehttpclient.execute (HttpGet);
- //Get Response message entity
- httpentity entity = httpresponse.getentity ();
- //Response status
- System.out.println ("Status:" + Httpresponse.getstatusline ());
- //Determine if the response entity is empty
- if (Entity! = null) {
- System.out.println ("contentencoding:" + entity.getcontentencoding ());
- SYSTEM.OUT.PRINTLN ("Response content:" + entityutils.tostring (entity));
- }
- } catch (IOException e) {
- E.printstacktrace ();
- } finally {
- try {
public class Httpclienttest {public static void main (String args[]) {//create Httpclientbuilder HTTPCLIENTBU Ilder Httpclientbuilder = Httpclientbuilder.create (); HttpClient closeablehttpclient closeablehttpclient = Httpclientbuilder.build (); HttpGet httpget = new HttpGet ("http://www.gxnu.edu.cn/default.html"); System.out.println (Httpget.getrequestline ()); try {//execute GET request HttpResponse HttpResponse = Closeablehttpclient.execute (HttpGet); Gets the response message entity httpentity entities = httpresponse.getentity (); Response Status System.out.println ("Status:" + Httpresponse.getstatusline ()); Determines whether the response entity is empty if (entity! = null) {System.out.println ("contentencoding:" + Entity.getcontentenco Ding ()); SYSTEM.OUT.PRINTLN ("Response content:" + entityutils.tostring (entity)); }} catch (IOException e) {e.printstacktrace (); } Finally {try { [Java]View Plaincopyprint?
- //Close stream and release resources
- Closeablehttpclient.close ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- }
Closes the stream and frees the resource closeablehttpclient.close (); } catch (IOException e) { e.printstacktrace ();} }} }
The following is from the HttpClient4.3.1 API Documentation: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/ Overview-summary.html
2, Httpclientbuilder
Httpclientbuilder is used to create closeablehttpclient instances. Look at the API documentation, Abstracthttpclient, autoretryhttpclient, defaulthttpclient, etc. are deprecated, using httpclientbuilder instead.
3, Closeablehttpclient
Implementation interface: Closeable, autocloseable, HttpClient; sub-class: Abstracthttpclient
4, HttpGet
Non-thread safe; HttpGet has three constructor methods: HttpGet (), HttpGet (String uri), HttpGet (URI Uri)
5, HttpResponse
The server returns an HTTP response message after receiving and interpreting the request
Response = Status-line * ((General-header | Response-header | entity-header) CRLF) CRLF [ Message-body]
HttpClient4.3.1 Simple Getting Started instance turn