First, introduce some good websites:
Introduction to httpclient:
HTTP client certificate import: http://www.blogjava.net/happytian/archive/2006/12/22/89447.html
Advanced knowledge of httpclient: http://laohuang.iteye.com/blog/55613
HTTP client official document: http://hc.apache.org/http components-client/index.html
HTTP client resource closure: http://www.iteye.com/topic/234759
And then, that's the sentence... Good things to reproduce, learn one can not be less, below Turn from here
I believe that the importance of the Http protocol does not need to say much. Compared with the URLConnection provided by traditional JDK, HttpClient adds ease of use and flexibility (specific differences, we will discuss later). It is not only easier for clients to send Http requests, but also for developers. test Interface (based on Http protocol) improves the efficiency of development and the robustness of code. Therefore, mastering HttpClient is a very important required content. After mastering HttpClient, I believe that the understanding of Http protocol will be more in-depth.
HttpClient is a sub-project of Apache Jakarta Common. It provides an efficient, up-to-date and feature-rich client programming toolkit to support HTTP protocol, and it supports the latest version and recommendations of HTTP protocol. HttpClient has been used in many projects, such as Apache Jakarta and two other well-known open source projects, Cactus and HTMLUnit, which use HttpClient.
Download address: http://hc.apache.org/downloads.cgi
1. Standard-based, pure Java Language. Implementation of Http1.0 and Http1.1
2. All methods of Http (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) are implemented with extensible object-oriented structure.
3. Support HTTPS protocol.
4. Establish transparent connections through Http proxy.
5. Use CONNECT method to establish tunnel https connection through Http proxy.
6. Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos authentication scheme.
7. Plug-in custom authentication scheme.
8. Portable and reliable socket factories make it easier to use third-party solutions.
9. Connection Manager supports multi-threaded applications. Supports setting the maximum number of connections, and also supports setting the maximum number of connections per host, discovering and closing outdated connections.
10. Automatically process Cookies in Set-Cookie.
12. Request's output stream prevents content from being buffered directly to the socket server.
13. Response's input stream can effectively read the corresponding content directly from the socket server.
14. KeepAlive is used to maintain persistent connections in HTTP 1.0 and HTTP 1.1.
15. Get the response code and headers sent by the server directly.
16. Set the connection timeout capability.
17. Experimental support http1.1 response caching.
18. The source code is freely available based on Apache License.
Using HttpClient to send requests and receive responses is very simple. Generally, the following steps are needed.
1. Create the HttpClient object.
2. Create an instance of the request method and specify the request URL. If you need to send a GET request, create the HttpGet object; if you need to send a POST request, create the HttpPost object.
3. If you need to send request parameters, you can call HttpGet and HttpPost common setParams(HetpParams params) method to add request parameters; for HttpPost objects, you can also call setEntity(HttpEntity entity) method to set request parameters.
4. Call the execute(HttpUriRequest request) of the HttpClient object to send the request, which returns a HttpResponse.
5. The server's response header can be obtained by calling the getAllHeaders(), getHeaders(String name) of HttpResponse, and the HttpResponse's getEntity() method can be used to obtain the HttpEntity object, which wraps the server's response content. The program can get the response content of the server through this object.
6. Release the connection. Whether or not the execution method succeeds, the connection must be released
This example uses the latest version of HttpClient 4.3. This version is quite different from the previous code style, we should pay more attention to it.
That's the same sentence... Good things to reproduce, learn one can not be less, below Turn from here
HttpClient has several points to pay attention to in its application:
Where HttpClient is generated:
First, you must have the following two parameter settings, otherwise you will generate multiple Cookie header s to the web server
ArrayList headerList=new ArrayList();
Header accept=new Header("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*");
Second: Where httpMethod is generated:
PostMethod postMethod =new PostMethod();
Above can avoid sending something like "HTTP://127.0.0.1:8080/action.do?" The URI error occurred when the content = 123456 "URL.
Third: The Connection Pool Timeout Exception problem may be caused by several reasons:
1. Setting connection timeout time is not set or too small;
2. Whether the connection after request has been released;
For more information, as well as parameter settings and code writing, risks, see This article