Java Implementation Web Landing
Last Update:2017-02-28
Source: Internet
Author: User
Web Web Landing is nothing more than Web Capture, cookie management, post and get-mode simulations.
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 ();
//analog into IE
connection.setrequestproperty ("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
Get Cookie:
httpurlconnection huc= (httpurlconnection) url.openconnection ();
InputStream is = Huc.getinputstream ();
//Get 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. Jcookie Package (http://jcookie.sourceforge.net/)
Get Cookies:
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);
the new request, using the cookie obtained above:
url = new URL ("http://www.site.com/");
Huc = (httpurlconnection) url.openconnection ();
client.setcookies (HUC, CJ);
Simulation
of
3.post mode
url url = new URL ("www.xyz.com");
httpurlconnection Huc = (httpurlconnection) url.openconnection ();
//settings allow output
Huc.setdooutput (TRUE);
//Set to post mode
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 (). GetBytes ("GBK"));
Os.close ();
bufferedreader br = new BufferedReader (New InputStreamReader (Huc.getinputstream ())
Huc.connect ();
String line = Br.readline ();
while (line!= null) {
L
System.out.printli (line);
line = Br.readline ();
}