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?
- URL url = new URL ("webpage ");
- Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
- Connection. setdooutput (true); // allow connection submission information
- Connection. setrequestmethod ("Post"); // webpage submission methods: "Get" and "Post"
- Connection. setrequestproperty ("User-Agent ",
"Mozilla/4.7 [En] (Win98; I )");
- Stringbuffer sb = new stringbuffer ();
- SB. append ("username = admin ");
- SB. append ("& Password = admin ");
- Outputstream OS = connection. getoutputstream ();
- OS. Write (sb. tostring (). getbytes ());
- OS. Close ();
- Bufferedreader BR = new bufferedreader (New inputstreamreader (connection. getinputstream ()));
- String responsecookie = connection. getheaderfield ("Set-cookie"); // obtain the cookie used
- System. Out. println ("Cookie:" + responsecookie );
- String line = Br. Readline ();
- While (line! = NULL ){
- System. Out. println (new string (line. getbytes ()));
- Line = Br. Readline (); // enter the logon webpage.
- }
- // Acces
- URL url1 = new URL ("webpage logon page ");
- Httpurlconnection connection1 = (httpurlconnection) url1.openconnection ();
- Connection1.setrequestproperty ("cookie", responsecookie); // send the cookie after login to the server
- Bufferedreader br1 = new bufferedreader (New inputstreamreader (connection1.getinputstream ()));
- String line1 = br1.readline ();
- While (line1! = NULL ){
- System. Out. println (new string (line1.getbytes ()));
- Line1 = br1.readline ();
- }
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.