The most frequently encountered problem in HTTP client programming is that the content of many websites is only visible to registered users, in which case you must require the correct username and password to log in successfully before you can browse to the desired page. Because the HTTP protocol is stateless, that is, the validity of the connection is limited to the current request, and the connection is closed after the request has finished. In this case, the cookie mechanism must be used in order to save the user's login information. In Jsp/servlet, for example, when a browser requests a JSP or a servlet page, the application server returns a parameter called Jsessionid (which varies depending on the application server), and the value is a long, unique string of cookies. This string value is the session ID that is currently accessing the site. The browser takes the cookie information such as Jsessionid on every other page of the site, and the application server obtains the corresponding session information based on reading the session ID.
For Web sites that require users to log on, the user profile is generally saved in the session of the server after the user has successfully logged in, so that when accessing other pages, the application server reads the session ID of the current request according to the cookie sent by the browser to obtain the corresponding session information. Then you can determine whether the user profile exists in the session information, if there is allowed to access the page, otherwise jump to the login page requires the user to enter the account number and password to sign in. This is generally used in the JSP development site in the processing of user login more common method.
In this way, for HTTP clients, if you want to access a protected page, you must simulate the work done by the browser, the first is to request the login page, and then read the cookie value, and then request the login page again to join each parameter required by the login page, and finally request the final page. Of course, in addition to the first request, other requests need to be accompanied by cookie information so that the server can determine whether the current request has been verified. Said so much, but if you use httpclient, you don't even need to add a line of code, you just have to pass the login information to perform the login process, and then directly access the desired page, and access to a normal page without any difference, because the class HttpClient has done all the work for you, and it's great! The following example implements the process of such an access.
package http.demo; import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.cookie.*;import org.apache.commons.httpclient.methods.*; /** * examples for demonstrating login forms * @author Liudong */public class formlogindemo { static final string logon_site = "localhost" ; static final int LOGON_PORT = 8080; public static void main (String[] args) throws Exception{ httpclient client = new httpclient (); client.gethostconfiguration (). Sethost (Logon_site, logon_port); // Demo Login Page login.jsp->main.jsp Postmethod post = new postmethod ( "/main.jsp " ); namevaluepair name = new Namevaluepair ( "name" , "LD" ); namevaluepair pass = new namevaluepair ( "password" , "ld" ); post.setrequestbody ( new namevaluepair[]{name,pass}); int status = client.executemethod (post); system.out.println ( Post.getresponsebodyasstring ()); post.releaseconnection (); // View cookie Information cookiespec cookiespec = cookiepolicy.getdefaultspec (); cookie[] Cookies = cookiespec.match (logon_site, logon_port, "/" , false , Client.getstate (). GetCookies ()); if (cookies.length == 0) { system.out.println ( "None" ); } else { for ( int i = 0; i < cookies.length; i++) { system.out.println (Cookies[i].tostring ()); } } // access to the required pages main2.jsp getmethodget=newgetmethod ("/main2.jsp"); client.executemethod (get); system.out.println ( Get.getresponsebodyasstring ()); get.releaseconnection (); } }
Reference article: http://my.oschina.net/u/218583/blog/42923
Get cookie security pass cognition after Android user operation Web Background login