HttpClient Complete Tutorial

Source: Internet
Author: User
Tags html form
Preface

The HTTP protocol should be the most important protocol in the Internet. The growing Web services, networked appliances, and so on, are inheriting and expanding the HTTP protocol, moving beyond the browser.

Although the java.net package in the JDK provides some basic methods for accessing network resources through the HTTP protocol, it is not flexible and powerful in most scenarios. HttpClient is dedicated to filling this gap by providing an efficient, up-to-date, and feature-rich package to implement HTTP clients.

To expand, HttpClient supports basic HTTP protocols and supports HTTP-AWARE client programs such as Web browsers, WebService clients, and distributed systems that use or to extend HTTP protocols.

1. HttpClient Range/Feature is a Httpcore based client HTTP Transport class library based on traditional (blocked) IO content Independent

2, httpclient can not do things HttpClient is not a browser, it is a client-side HTTP protocol Transport class library. HttpClient is used to send and receive HTTP messages. HttpClient does not process the contents of HTTP messages, does not do JavaScript parsing, does not care about the content type, and if there is no explicit setting, HttpClient will not format the request, redirect the URL, or any other function associated with HTTP message transfer.

Chapter I Basic Concepts 1.1. Request for Implementation

The most basic function of httpclient is to execute the HTTP method. The execution of an HTTP method involves the interaction of one or more HTTP requests/http responses, which are usually handled automatically by httpclient and transparently to the user. The user only needs to provide the HTTP request object, HttpClient sends the HTTP request to the target server, and the receiving server responds, if the HTTP request execution is unsuccessful, the httpclient throws a strange.

Here is a very simple example of HTTP request execution:

Closeablehttpclient httpclient = Httpclients.createdefault ();   HttpGet httpget = new HttpGet ("http://localhost/");   Closeablehttpresponse response = Httpclient.execute (HttpGet);   try {<...>} finally {response.close (); }

1.1.1. HTTP request

All HTTP requests have a request line, including the method name, the URI of the request, and the HTTP version number.

HttpClient supports all HTTP methods defined by this version of http/1.1: Get,head,post,put,delete,trace and options. For each HTTP method, HttpClient defines a corresponding class: HttpGet, Httphead, HttpPost, Httpput, Httpdelete, HttpTrace, and Httpopquertions.

Request-uri is the Uniform Resource Locator that identifies the resources in the HTTP request. The Http request URIs contains the protocol name, host name, host port (optional), resource path, query (optional), and fragment information (optional).

HttpGet httpget = new HttpGet ("Http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f &oq= ");

HttpClient provides UriBuilder tool classes to simplify the process of creating and modifying URIs.

Uri uri = new UriBuilder (). SetScheme ("http"). Sethost ("www.google.com"). SetPath ("/search")  . Setparameter ("Q", "HttpClient"). Setparameter ("btng", "Google Search"). Setparameter ("AQ",   "F"). Setparameter ("OQ", ""). Build ();   HttpGet httpget = new HttpGet (URI); System.out.println (Httpget.geturi ());

The above code is output in the console:

http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=

1.1.2. HTTP response

When the server receives the HTTP request from the client, it parses it and sends the response to the client, which is the HTTP response. The first line of the HTTP response is the protocol version, followed by the numeric status code and the associated text segment.

HttpResponse response = new Basichttpresponse (httpversion.http_1_1, HTTPSTATUS.SC_OK, "OK");   System.out.println (Response.getprotocolversion ());   System.out.println (Response.getstatusline (). Getstatuscode ());   System.out.println (Response.getstatusline (). Getreasonphrase ()); System.out.println (Response.getstatusline (). toString ());
The above code is output in the console:

http/1.1 http/1.1 OK

1.1.3. Message header

An HTTP message can contain a series of headers to describe the HTTP message, such as message length, message type, and so on. HttpClient provides methods to obtain, add, remove, and enumerate message headers.

HttpResponse response = new Basichttpresponse (httpversion.http_1_1, HTTPSTATUS.SC_OK, "OK"); Response.AddHeader ("Set-cookie", "C1=A; path=/;   Domain=localhost "); Response.AddHeader ("Set-cookie", "C2=B; Path=\ "/\", c3=c;   Domain=\ "localhost\");   Header H1 = Response.getfirstheader ("Set-cookie");   SYSTEM.OUT.PRINTLN (H1);   Header H2 = Response.getlastheader ("Set-cookie");   System.out.println (H2);   Header[] hs = response.getheaders ("Set-cookie"); System.out.println (hs.length);
The above code is output in the console:

Set-cookie:c1=a; path=/; Domain=localhost set-cookie:c2=b; Path= "/", c3=c; domain= "localhost" 2

The most efficient way to get a message header of a specified type is to use the Headeriterator interface.

HttpResponse response = new Basichttpresponse (httpversion.http_1_1, HTTPSTATUS.SC_OK, "OK"); Response.AddHeader ("Set-cookie", "C1=A; path=/;   Domain=localhost "); Response.AddHeader ("Set-cookie", "C2=B; Path=\ "/\", c3=c;      Domain=\ "localhost\");      Headeriterator it = response.headeriterator ("Set-cookie");   while (It.hasnext ()) {System.out.println (It.next ()); }

The above code is output in the console:

Set-cookie:c1=a; path=/; Domain=localhost set-cookie:c2=b; Path= "/", c3=c; domain= "localhost"

Headeriterator also provides a very convenient way to parse HTTP messages into separate message header elements.

  Httpresponse response = new basichttpresponse (httpversion.http_1_1,         HttpStatus.SC_OK,  "OK");   Response.AddHeader ("Set-cookie",          "C1=a; path=/; domain=localhost");   Response.AddHeader ("Set-cookie",         "c2=b; path=\"/\ ",  c3=c; domain=\" Localhost\ "");      headerelementiterator it = new  Basicheaderelementiterator (       response.headeriterator ("Set-cookie"));       while  (It.hasnext ())  {       headerelement elem  = it.nextelement ();        system.out.println (Elem.getName ()  +  " = "  + elem.getvalue ());       namevaluepair[]  params = elem.getparameters ();       for  (int i = 0; i <  params.length; i++)  {           system.out.println ( " "  + params[i]);       }  }  

The above code is output in the console:

C1 = a path=/domain=localhost C2 = b path=/C3 = C Domain=localhost

1.1.4. HTTP entity

HTTP messages can carry HTTP entities, which can be either HTTP requests or HTTP responses. HTTP entities, which can be found in some HTTP requests or responses, but are not required. Two methods are defined in the HTTP specification that contain the request: Post and put. HTTP responses typically contain a content entity. Of course, there are exceptions to this rule, such as the head method response, 204 no content, 304 no modification or 205 content resource resets.

The httpclient divides three different HTTP entity contents according to the different sources. streamed flow: content is accepted by flow or generated in run. In particular, streamed this class contains the entity content obtained from the HTTP response. Generally speaking, streamed entities are not repeatable. self-contained self-contained: content is obtained in memory or through independent connections or other entities. The entity content of the self-contained type is usually repeatable. This type of entity is typically used to turn off HTTP requests. Wrapping Wrapper: This type of content is obtained from another HTTP entity.

When reading content from an HTTP response, the above three distinctions are important to the connection manager. The difference between the two types of streamed and self-contained is less important for request entities created by the application and only sent using HttpClient. In this case, it is recommended that you consider an streamed entity, such as a streaming type, and a self-contained self contained entity that can be duplicated. 1.1.4.1. Repeatable entities

An entity is repeatable, meaning that its contents can be read multiple times. This multiple-read only entity that self contained (self-contained) can do (such as bytearrayentity or stringentity). 1.1.4.2. Using HTTP Entities

Because an HTTP entity can represent both binary content and textual content, HTTP entities Support character encoding (in order to support the latter, that is, text content).

HTTP entities are created when an HTTP request that needs to perform a complete content or an HTTP request has succeeded, and the server sends a response to the client.

If you want to read content from an HTTP entity, we can take advantage of the GetContent method of the Httpentity class to get the input stream (Java.io.InputStream) of the entity, or take advantage of the writeto of the Httpentity class ( OutputStream) method to get the output stream, which writes all the content to the given stream.
When an entity class has been accepted, we can use the getContentType () and Getcontentlength () methods of the Httpentity class to read the Content-type and content-length two header messages (if any). Because Content-type contains mime-types character encodings, such as the Text/plain or Text/html,httpentity class, the Getcontentencoding () method reads the encoding. If the header information does not exist, Getcontentlength () returns -1,getcontenttype () and returns NULL. If the Content-type information exists, a header class is returned.

When you create an HTTP entity for sending messages, you need to attach meta information at the same time.

Stringentity myentity = new Stringentity ("Important Message", Contenttype.create ("Text/plain", "UTF-8"));   System.out.println (Myentity.getcontenttype ());   System.out.println (Myentity.getcontentlength ());   System.out.println (entityutils.tostring (myentity)); System.out.println (Entityutils.tobytearray (myentity). length);
The above code is output in the console:

Content-type:text/plain; Charset=utf-8 Important Message 17

1.1.5. Ensure that the underlying resource connection is freed

To ensure that system resources are released correctly, we either manage the content flow of the HTTP entity or turn off the HTTP response.

  Closeablehttpclient httpclient = httpclients.createdefault ();   HttpGet  Httpget = new httpget ("http://localhost/");   Closeablehttpresponse response  = httpclient.execute (httpget);   try {       httpentity  entity = response.getentity ();       if  (entity !=  null)  {           inputstream instream =  entity.getcontent ();           try {               // do something useful            } finally {                instream.close ();            }       }  } finally {        response.close ();  }  
The difference between shutting down the HTTP entity content stream and closing the HTTP response is that the former maintains the associated HTTP connection by consuming the HTTP entity content, which then shuts down and discards the HTTP connection immediately.

Note the WriteTo (OutputStream) method of Httpentity, which also ensures that system resources are freed when HTTP entities are written to OutputStream. If this method calls the Httpentity getcontent () method, then it has a java.io.InpputStream instance, and we need to close the stream in finally.

But there is a situation where we only need to get a small portion of the HTTP response content, and the repeatability of getting the entire content and realizing the connection is too costly, and we can turn off the content input and output stream by closing the response.

  Closeablehttpclient httpclient = httpclients.createdefault ();   HttpGet  Httpget = new httpget ("http://localhost/");   Closeablehttpresponse response  = httpclient.execute (httpget);   try {       httpentity  entity = response.getentity ();       if  (entity !=  null)  {           inputstream instream =  entity.getcontent ();           int byteone =  instream.read ();           int bytetwo =  instream.read ();           // Do not  need the rest       }  } finally {       rEsponse.close ();  }  
After the above code executes, the connection becomes unavailable and all resources are freed.

1.1.6. Consuming HTTP entity Content

HttpClient recommends using the Httpentity getconent () method or the Httpentity WriteTo (OutputStream) method to consume HTTP entity content. HttpClient also provides the Entityutils class, which provides static methods to make it easier to read the contents and information of HTTP entities. Entityutils provides methods that can read HTTP entities as strings or as byte arrays, compared to how content is read in Java.io.InputStream streams. However, it is strongly deprecated to use the Entityutils class unless the response from the target server is trustworthy and the HTTP response entity is not too long.

  Closeablehttpclient httpclient = httpclients.createdefault ();   HttpGet  Httpget = new httpget ("http://localhost/");   Closeablehttpresponse response  = httpclient.execute (httpget);   try {       httpentity  entity = response.getentity ();       if  (entity !=  null)  {           long len =  Entity.getcontentlength ();           if  (len !=  -1 && len < 2048)  {                system.out.println (Entityutils.tostring (entity));            } else {                // stream content out           }        }  } finally {        Response.close ();  }  

In some cases, we would like to read the contents of the HTTP entity repeatedly. This requires the HTTP entity content to be cached on memory or on disk. The easiest way to do this is to convert the HTTP entity into bufferedhttpentity, which buffers the contents of the original HTTP entity into memory. Later we can read the contents of the bufferedhttpentity repeatedly.

Closeablehttpresponse response = <...> httpentity entity = response.getentity ();   if (entity!= null) {entity = new bufferedhttpentity (entity); }

1.1.7. Create HTTP Entity content

HttpClient provides classes that enable efficient output of HTTP entity content over HTTP connections. HttpClient provides the common data types that are covered by these classes, such as string,byte arrays, input streams, and file types: stringentity,bytearrayentity,inputstreamentity,fileentity.

File File = new file ("Somefile.txt");              fileentity entity = new Fileentity (file, Contenttype.create ("Text/plain", "UTF-8"));   HttpPost HttpPost = new HttpPost ("http://localhost/action.do");   Httppost.setentity (entity); Note that because inputstreamentity can only be read once from the underlying stream of data, it cannot be duplicated. It is recommended that by inheriting httpentity this self-contained class derives from the definition of the Httpentity class, rather than directly using the Inputstreamentity class. Fileentity is a good starting point (Fileentity is the inherited httpentity).

1.7.1.1. HTML form

Many applications need to simulate the process of submitting HTML forms, for example, to log on to a Web site or submit input to the server. HttpClient provides a urlencodedformentity class to help implement this process.   list<namevaluepair> formparams = new arraylist<namevaluepair> ();   Formparams.add (New Basicnamevaluepair ("param1", "value1"));   Formparams.add (New Basicnamevaluepair ("param2", "value2"));   urlencodedformentity entity = new Urlencodedformentity (Formparams, consts.utf_8);   HttpPost HttpPost = new HttpPost ("http://localhost/handler.do"); Httppost.setentity (entity);

The urlencodedformentity instance encodes our parameters using the so-called URL encoding, resulting in the following results: Param1=value1&m2=value2

1.1.7.2. Content chunking

In general, it is recommended that httpclient itself choose the most appropriate transmission encoding based on the characteristics of the HTTP message delivery. Of course, if manual control is also possible, you can set the Httpentity

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.