Java.net simulates user login to the webpage and maintains the session

Source: Internet
Author: User

Sometimes, you may need to use Java programs to access the webpage. There are many programs that can normally access the webpage, and there is nothing to talk about. Here we mainly explain how to use Java to access the webpage, how to keep the logon program when you need to log on.

First, let's briefly explain the principle: in fact, the webpage and the server are two sets of applications, and the client accesses the server once. In fact, after the webpage is obtained, the connection will be disconnected immediately. In this case, it indicates that the server will not maintain a long-Term connection, but the client seems to keep the connection when logging on to the server in real time. This situation is different from the previous mode; of course not, disconnecting is the fundamental principle of the entire B/S architecture. Therefore, how to maintain these connections is actually using cookies. when accessing a webpage, each time the webpage server generates a cookie, record a key value of the current webpage. If you log on, the session on the server will be connected to a cookie on the client, in this way, the login simulation can be completed by sending the session ID recorded by the client cookie to this page.
The Code is as follows:

[Java]
View plaincopyprint?
  1. URL url = new URL ("webpage ");
  2. Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
  3. Connection. setdooutput (true); // allow connection submission information
  4. Connection. setrequestmethod ("Post"); // webpage submission methods: "Get" and "Post"
  5. Connection. setrequestproperty ("User-Agent ",
    "Mozilla/4.7 [En] (Win98; I )");
  6. Stringbuffer sb = new stringbuffer ();
  7. SB. append ("username = admin ");
  8. SB. append ("& Password = admin ");
  9. Outputstream OS = connection. getoutputstream ();
  10. OS. Write (sb. tostring (). getbytes ());
  11. OS. Close ();
  12. Bufferedreader BR = new bufferedreader (New inputstreamreader (connection. getinputstream ()));
  13. String responsecookie = connection. getheaderfield ("Set-cookie"); // obtain the cookie used
  14. System. Out. println ("Cookie:" + responsecookie );
  15. String line = Br. Readline ();
  16. While (line! = NULL ){
  17. System. Out. println (new string (line. getbytes ()));
  18. Line = Br. Readline (); // enter the logon webpage.
  19. }
  20. // Acces
  21. URL url1 = new URL ("webpage logon page ");
  22. Httpurlconnection connection1 = (httpurlconnection) url1.openconnection ();
  23. Connection1.setrequestproperty ("cookie", responsecookie); // send the cookie after login to the server
  24. Bufferedreader br1 = new bufferedreader (New inputstreamreader (connection1.getinputstream ()));
  25. String line1 = br1.readline ();
  26. While (line1! = NULL ){
  27. System. Out. println (new string (line1.getbytes ()));
  28. Line1 = br1.readline ();
  29. }

URL url = new URL ("webpage"); <br/> httpurlconnection connection = (httpurlconnection) URL. openconnection (); <br/> connection. setdooutput (true); // allow connection submission information <br/> connection. setrequestmethod ("Post"); // webpage submission methods: "Get" and "Post" <br/> connection. setrequestproperty ("User-Agent", "Mozilla/4.7 [En] (Win98; I)"); <br/> stringbuffer sb = new stringbuffer (); <br/> Sb. append ("username = admin"); <br/> Sb. append ("& Password = admin "); <Br/> outputstream OS = connection. getoutputstream (); <br/> OS. write (sb. tostring (). getbytes (); <br/> OS. close (); </P> <p> bufferedreader BR = new bufferedreader (New inputstreamreader (connection. getinputstream (); </P> <p> string responsecookie = connection. getheaderfield ("Set-cookie"); // obtain the cookie used <br/> system. out. println ("Cookie:" + responsecookie); <br/> string line = BR. readline (); </P> <p> while (line! = NULL) {</P> <p> system. out. println (new string (line. getbytes (); </P> <p> line = BR. readline (); // enter the logon webpage </P> <p >}< br/> // acces <br/> URL url1 = new URL ("webpage logon page "); <br/> httpurlconnection connection1 = (httpurlconnection) url1.openconnection (); <br/> connection1.setrequestproperty ("cookie", responsecookie ); // send the cookie after login to the server <br/> bufferedreader br1 = new bufferedreader (New inputstreamreader (connection1.g Etinputstream (); </P> <p> string line1 = br1.readline (); </P> <p> while (line1! = NULL) {</P> <p> system. out. println (new string (line1.getbytes (); </P> <p> line1 = br1.readline (); </P> <p >}</P> <p>
However, many websites now use the webpage token mechanism, which means that each page generates a unique key value and the login verification code is added, in this way, many web page programs can no longer simply use this web page to log on, but this mechanism allows everyone to understand how to maintain the client session connection process.

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.