Java implements web landing
Web landing is nothing more than web access, cookie management, POST, and GET mode simulation. 1. Web content acquisition java.io.inputstream in; java.net.URL URL = new java.net.url (www.xyz.com/content.html); java.net.httpurlconnection connection = (java.net.httpurlConnection URL.OpenConnection (); connection = (java.net.httpurlconnection) URL.OpenConnection (); // simulation into IE Connection.seueStproperty ("User-Agent", "Mozilla / 4.0 (compatible; msie 6.0; windows 2000) "); connection.connect (); in = connection.getInputStream (); java.io.BufferedReader breader = new BufferedReader (new InputStreamReader (in," GBK ")); String str = breader.readLine ()); while ( St! = null) {system.out.println (STR); str = BReader.Readline ());} 2.cookie management
1. Direct way to get cookie: httpurlconnection huc = (httpurlconnection) URL.OpenConnection ();
InputStream IS = huc.getinputstream ();
// get the sessionid.
String cookieval = hc.getheaderfield ("set-cookie");
String sessionid;
IF (CookieVal! = NULL)
{
SessionId = cookieval.substring (0, cookieval.indexof (";"));
}
Send settings cookie:
HTTPURLCONNECTION HUC = (httpurlconnection) URL.OpenConnection ();
IF (sessionid! = NULL)
{
HUC.SetRequestProperty ("cookie", sessionid);
}
InputStream IS = huc.getinputstream ();
2. Get the cookie (http://jcookie.sourceforge.net/ http://jcookie.sourceforge.net/):
URL URL = New URL ("http://www.site.com/");
HTTPURLCONNECTION HUC = (httpurlconnection) URL.OpenConnection ();
HUC.CONNECT ();
InputStream IS = huc.getinputstream ();
Client Client = New Client ();
Cookiejar CJ = Client.getCookies (HUC);
New requests, using the cookie obtained above:
URL = New URL ("http://www.site.com/"); huc = (httpurlConnection) URL.OpenConnection ();
Client.SetCookies (HUC, CJ);
3. The Simulation URL URL = New URL ("www.xyz.com"); httpurlconnection huc = (httpurlconnection) URL.OpenConnection (); // Setting Allow Output Huc.SetDoOutput (TRUE); // Set to POST HUC.SETREQUESTMETHOD ("POST"); HUC.SetRequestProperty ("User-Agent", "Mozilla / 4.7 [EN] (WIN98; I)"); StringBuffer SB = New StringBuffer (); sb.append ("UserName = " usernme); sb.append (" & password = " password); // POST information OutputStream OS = huc.getOutputStream (); os.write (sb.toString (); Gettes (" GBK "); OS. Close (); bufferedreader br = new bufferedreader (new inputstreamreader (huc.getinputstream ())))
HUC.CONNECT (); string line = br.readline ();
While (line! = null) {
Lsystem.out.printli (line);
Line = br.readline ();
}
Conclusion: The above code can basically implement the function of the website, information acquisition. Seeing the software sold in the landslide and other software, we also do it. Reference: 1.a recipe for cookie management http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-cookie .html