Although JDK's java.net package provides basic functions for accessing the HTTP protocol, for most applications, the functions provided by the JDK library itself are not rich and flexible. Httpclient is a sub-project under Apache Jakarta common. It is used to provide an efficient, up-to-date, and function-rich client programming toolkit that supports http protocol. It also supports the latest versions and suggestions of HTTP protocol. Httpclient has been applied in many projects, such Apache Jakarta uses httpclient for two other open-source projects, cactus and htmlunit. The httpclient project is very active and many people are using it. Currently, the httpclient version is 3.0 RC4 released in 5.10.11.The main functions of httpclient are as follows: 1) Implement all HTTP methods (get, post, put, Head, etc ); 2) automatic steering is supported; 3) supports HTTPS; 4) supports proxy servers. 3.1 environment construction and required packages
Java Development Environment JDK is required, and network access is required. The android program requires a permission of "android. Permission. Internet. Required packages: 1. commons-httpclient-3.1.jar: includes the classes required for the HTTP protocol. 2. commons-logging-1.1.jar: includes a class that records activity logs when the program is running. 3, commons-codec-1.3.jar: including encoding and decoding class. These packages are open-source projects of Apache. You can find them at http://www.apache.org. 3.2 httpclient implements basic HTTP Communication Operations
Before you implement all the operations, you must first instantiate an httpclient, that is, initialize a client. Httpclient client = new httpclient ();3.2.1 request Take the GET request as an example.
A. instantiate a request method. Httpmethod method = new getmethod ("http://www.google.cn "); Note:
① Although Google has moved the server out of mainland China, httpclient can achieve automatic redirection. So when the status code returned by the server is 3××, it will be automatically redirected to know the actual location of the file ). ② The string in the getmethod constructor represents the URI address of the file. The complete name is required because no server host address is specified. You can also do this: Client. gethostconfiguration (). sethost ("www.imobile.com.cn", 80, "HTTP ");
......
Httpmethod method = new getmethod ("/simcard. php? Simcard = 1330227 "); B. Add the desired message header. Method. addrequestheader ("range", "bytes = 500 -"); Httpclient will construct the necessary message header information. If there are no special requirements, you do not need to modify it. However, if you need to add some special information in the message header, such as resumable upload during download, you can use the above method to modify it.
C. Send a request (execute a command ). Int statuscode = client.exe cutemethod (method ); At this point, the program actually sends a request to the server. After the connection is successful, the function returns, and the return value is the status code.
3.2.2 response
Example. A. Return status code. In the preceding example, "statuscode" is the status code. In addition, you can: Int statuscode = method. getstatuscode ();Note: there is an "httpstatus" Class in the httpclient package, which defines most status codes. For example: Httpstatus. SC _ OK Httpstatus. SC _forbidden. B. response header. Header [] headers = method. getresponseheaders ();Obtains the Response Headers returned by all servers. Header header = method. getrequestheader ("Content-Type ");Obtains the key-value pairs specified in the response header. You can call header. getname () and header. getvalue () to obtain relevant information. C. Response body. Byte [] bytes = method. getresponsebody ();Inputstream = method. getresponsebodyasstream ();
String string = method. getresponsebodyasstring ();The above three methods are selected as needed. 3.2.3 disconnect Method. releaseconnection (); Disconnect.
3.2.4 others Others include things that have nothing to do with downloads, but are very basic and useful. A. Post Data. The post request and get request are roughly the same. The only thing you need to note is how to add the information you need to transmit to the post information. Postmethod. setrequestbody (inputstream body );
Postmethod. setrequestbody (namevaluepair [] parameterbody );
Postmethod. setrequestbody (string body );B. proxy server. You only need to specify the proxy of the httpclient instance. All operations based on this instance will be performed by this proxy.
Httpclient. gethostconfiguration (). setproxy (hostname, Port ); C. character encoding.
The encoding of a target page may appear in two places: The first part is the HTTP header returned by the server (Content-Type and content-encoding fields of requestheader ); The other part is the HTML/XML page. For example: <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> Or <? XML version = "1.0" encoding = "gb2312"?> D. Automatic jump. Httpclient can automatically redirect GET requests. However, automatic redirection is not supported for post and put requests that require subsequent services. When the status code returned by the server is 3××, You Need To jump Based on the address of the "location" field in the message header. Note that the address of the "location" field may be a relative address and must be processed by yourself. Another possibility is page navigation. For example, in HTML, <meta http-equiv = "refresh" content = "5; url = http://www.ibm.com/us">. E. HTTPS protocol. See:Httpclient getting started.
References
There are not many things in this article, most of which are excerpted, referenced, and summarized from some online materials. I would like to thank the authors of these articles for sharing their documents. 1,HTTP protocol (favorites) 2,File Download Principles 1 HTTP protocol 3,Httpclient getting started 4,Httpclient getting started tutorial
Reprinted please indicate the source:Http://www.blogjava.net/zh-weir/archive/2010/05/02/319892.html |