httpclient 4.3 tutorial

Source: Internet
Author: User
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 (

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.