In general, we all use IE or Navigator browsers to access a web server to browse the page view information or submit some data. These pages accessed are only some of ordinary pages, and some require users to log in, or require authentication, and some by encryption, such as HTTPS. The browser we currently currently does not constitute a problem. However, you may need to visit some pages through the program at some point, such as "stealing" from someone else's webpage; use the page provided by some sites to complete some feature, for example, we want to know a mobile phone. The number of the number and we have no such data, so I have to complete this feature with some websites of other companies. At this time, we need to submit your mobile phone number to the web page and resolve the data we want from the return page. . If the other party is just a very simple page, then our program will be very simple. This article is not necessary to have a lot of money here. However, considering some service authorization issues, many companies are often accessible by a simple URL, but must be registered and then logged in, you can use the pages that provide services, this time involves the cookie problem. deal with. We know that current popular dynamic web technology such as ASP, JSP is not to handle session information by cookie. In order for our program to use the service page provided by others, ask the program first log in to access the service page, this process needs you to process cookies yourself, think about how you use java.net.httpurlconnection to complete these features Horror things! Moreover, this is just a very common "stubborn" in our stubborn web server! If you have been uploaded by HTTP? No headache, these issues have "it" is very easy to solve!
We can't list all possible stubborn, we will handle several most common issues. Of course, as mentioned earlier, if we use java.net.httpurlconnection to get these questions is a terrible thing, so we will introduce an open source project before starting, this project is Apache Open Source Organization. HTTPCLIENT, it is part of Jakarta's Commons project, and the current version is 2.0RC2. COMMONS has already had a NET sub-project, but HTTPCLIENT is separately raised, and it can be seen that the HTTP server is not easy.
The Commons-HttpClient project is specially designed to simplify the HTTP client to make a variety of communication programming. Through it makes it easy to solve the original headache now, for example, you no longer manage the communication method of http or https, telling it that you want to use HTTPS mode, the rest is handed over to HTTPClient for you. This article will provide you with HTTPClient to solve them for several issues that we often encounter while writing HTTP client programs. In order to make readers faster familiar with this project, we will give a simple example to read The content of a web page, then step by step into all the problems in the advancement.
1. Read web page (http / https) content
Here is a simple example we give to access a page.
/ *
* Created on 2003-12-14 by liudong
* /
Package http.demo;
Import java.io.ioException;
Import org.apache.commons.httpclient. *;
Import org.apache.commons.httpclient.methods. *;
/ **
* The simplest HTTP client, used to demonstrate access to a page via GET or POST
* @Author liudong
* /
Public class SimpleClient {
Public static void main (string [] args) throws oException {
HTTPCLIENT Client = New httpclient ();
/ / Set proxy server address and port
//client.gethostconfiguration( ).SetProxy ("proxy_host_addr", proxy_port);
// Use a get method, if the server needs to connect through the HTTPS, then you only need to change the HTTP in the URL to https
HttpMethod method = New getMethod ("http://java.sun.com");
// use the POST method
// httpmethod method = new postmethod ("http://java.sun.com");
Client.executeMethod (METHOD);
// Print the status of the server returns
System.out.println (Method.getStatusline ());
// Print the information returned
System.out.println (Method.getResponsebodyAsstring ());
// Release connection
Method.releaseConnection ();
}
In this example, an instance of an HTTP client (httpclient) is first created, then the method of submit is GET or POST, and finally the submitted method is performed on the HTTPClient instance, and finally read the server feedback from the selected submission method. result. This is the basic process using HTTPClient. In fact, use a line of code to get the process of the entire request, very simple!
2. Submitting parameters to the web page in GET or POST, in the first one, we have already introduced how to use the GET or POST method to request a page, this section is different from the parameters required to set the page when submitted. We know if it is a request method, then all parameters are placed directly to the URL of the page with the question mark and the page address, each parameter is used, for example: http: //java.sun.com? Name = liudong & mobile = 123456, but it will have a little bit of trouble when using a Post method.
Example of this section demonstrates how to query the city where the mobile phone number is located, the code is as follows: / * Created on 2003-12-7 by liudong * / package http.demo; import java.io.ioException; import org.apache.commons. HttpClient. *; import org.apache.commons.httpclient.methods. *; / ** * Submit Parameter Demo * This program is connected to a page for querying the location of the mobile phone number * to query the query number segment 1330227, the province and city * @author Liudong * / public class SimpleHttpClient {public static void main (String [] args) throws IOException {HttpClient client = new HttpClient ();. client.getHostConfiguration () setHost ( "www.imobile.com.cn", 80 , "http"); httpmethod method = getPostMethod (); // Submit data client.executeMethod (Method) using POST; // The status returned by the print server system.out.println (Method.getStatusline ()); // Print Results page string response = new string (). GetBytes ("8859_1")); // Print the information system.out.println (response); method.releaseConnection ();} / ** * Using GET Way Submitted Data * @return * / Private Static HttpMethod Ge TgetMethod () {RETURN NEW GETMETHOD ("/ simcard.php? simcard = 1330227");} / ** * Submit data using POST * @Return * / private static httpmethod getPostMethod () {PostMethod Post = new PostMethod ("/ Simcard.php "); NameValuePair Simcard = New NameValuePair (" SimCard "," 1330227 "); post.setRequestBody (new nameValuePair [] {simcard}); return post;}} In the above example, page http: // www .imobile.com.cn / simcard.php Requires a parameter is Simcard, this parameter value is the mobile phone number segment, that is, the first seven digits of the mobile phone number, the server returns the province, city, and other detailed information corresponding to the submitted mobile phone number. GET's submission method only needs to add parameter information after the URL, and POST needs to set the parameter name and the value it corresponds to the value of 3. Handling page redirection