Interactive learning with Android and background data

Source: Internet
Author: User

abstract Any system can manipulate data without user login is very dangerous, link client and server is the user login session, after the user login system there is a SessionID, this SessionID is the client's cookie, After the client obtains the SessionID setting cookie, it can initiate any HTTP server request.

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.

1  PackageHttp.demo;2 Importorg.apache.commons.httpclient.*;3 ImportOrg.apache.commons.httpclient.cookie.*;4 Importorg.apache.commons.httpclient.methods.*; 5   6 /** 7 * Examples to demonstrate login forms8  * @authorLiudong9  */Ten  Public classFormlogindemo { One    Static FinalString logon_site = "localhost" ; A    Static Final intLogon_port = 8080; -    -     Public Static voidMain (string[] args)throwsexception{ theHttpClient client =NewHttpClient (); - client.gethostconfiguration (). Sethost (Logon_site, logon_port); -    -       //Impersonation Login Page login.jsp->main.jsp +Postmethod post =NewPostmethod ("/main.jsp" ); -Namevaluepair name =NewNamevaluepair ("name", "LD" ); +Namevaluepair pass =NewNamevaluepair ("Password", "LD" ); APost.setrequestbody (Newnamevaluepair[]{name,pass}); at       intStatus =Client.executemethod (POST); - System.out.println (post.getresponsebodyasstring ()); - post.releaseconnection (); -    -       //View cookie Information -Cookiespec Cookiespec =Cookiepolicy.getdefaultspec (); incookie[] cookies = Cookiespec.match (Logon_site, Logon_port, "/",false, Client.getstate (). GetCookies ()); -       if(Cookies.length = = 0) { toSystem.out.println ("None" ); +}Else { -           for(inti = 0; i < cookies.length; i++) { the System.out.println (cookies[i].tostring ()); *          } $       }Panax Notoginseng    -       //visit the desired page main2.jsp theGetmethodget=newgetmethod ("/main2.jsp"); + Client.executemethod (get); A System.out.println (get.getresponsebodyasstring ()); the get.releaseconnection (); +}

Reference: http://my.oschina.net/u/218583/blog/42923

Interactive learning with Android and background data

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.