To capture some Web privacy data, You need to log on first before you can obtain the data. To achieve this using a program, you need to implement automatic login and then save the login information in the cookie, in order to obtain data, you do not need to log on again. Take Netease mailbox as an example:
Required jar packages: commons-codec-1.3.jar, common-httpclient-3.0.1.jar, common-logging-1.1.1.jar, jaxen-1.1-beta-6.jar (not sure if necessary)
Source code:
Import java. Io. ioexception;
Import java. util. date;
Import org. Apache. commons. httpclient. Cookie;
Import org. Apache. commons. httpclient. httpclient;
Import org. Apache. commons. httpclient. httpexception;
Import org. Apache. commons. httpclient. namevaluepair;
Import org. Apache. commons. httpclient. Cookie. cookiepolicy;
Import org. Apache. commons. httpclient. Methods. getmethod;
Import org. Apache. commons. httpclient. Methods. postmethod;
Public class autologin {
Private Static string username = "***";
Private Static string loginurl = "http://reg.163.com/login.jsp ";
Private string cookiestr;
Private date endtime;
Public void autologin (){
Httpclient = new httpclient ();
Postmethod post = new postmethod (loginurl );
// Note the address here!
Post. setRequestHeader ("Accept-language", "En-US, en; q = 0.5 ");
Post. setRequestHeader ("Accept-encoding", "gzip, deflate ");
Post. setRequestHeader ("User-Agent", "Mozilla/5.0 (windows; U;
Windows NT 5.2; en-US; RV: 1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (. NET
CLR 3.5.30729 )");
Post. addrequestheader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8 ");
Namevaluepair user = new namevaluepair ("user ","***");
Namevaluepair Pwd = new namevaluepair ("password ","***");
Namevaluepair username = new namevaluepair ("username", username + "@ 163.com ");
Post. setrequestbody (New namevaluepair [] {username, user, PWD });
Httpclient. getparams (). setcookiepolicy (cookiepolicy. browser_compatibility );
Int status;
Try {
Status = httpclient.exe cutemethod (post );
System. Out. println ("loginurl:" + status );
Cookie [] cookies = httpclient. getstate (). getcookies ();
// Obtain the cookie
If (cookies. Length = 0 ){
System. Out. println ("Cookie: none ");
} Else {
If (cookies. length> = 2)
{
Endtime = Cookies [1]. getexpirydate ();
For (cookie C: cookies ){
Cookiestr + = C. tostring () + ";";
}
}
}
System. Out. println (endtime );
System. Out. println (cookiestr );
System. Out. println (post. getresponsebodyasstring ());
} Catch (httpexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Finally {
Post. releaseconnection ();
}
}
Public String getcontent (){
Date = new date ();
String content = NULL;
// The value of cookiestr is null or the cookie has expired.
If (cookiestr = "" | endtime = NULL | date. gettime ()> endtime. gettime ())
{
Autologin ();
}
Httpclient client = new httpclient ();
Client. getparams (). setcookiepolicy (cookiepolicy. browser_compatibility );
// Get Data URL
String url = "*******";
Getmethod get = new getmethod (URL );
Get. setRequestHeader ("Accept-language", "En-US, en; q = 0.5 ");
Get. setRequestHeader ("Accept-encoding", "gzip, deflate ");
Get. setRequestHeader ("accept", "text/html, application/XHTML + XML, application/XML; q = 0.9, */*; q = 0.8 ");
Get. setRequestHeader ("Accept-charset", "ISO-8859-1, UTF-8; q = 0.7, *; q = 0.7 ");
Get. setRequestHeader ("User-Agent", "Mozilla/5.0 (windows; U; Windows NT
5.2; en-US; RV: 1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (. NET CLR
3.5.30729 )");
// Set cookie, required
Get. setRequestHeader ("cookie", cookiestr );
Try {
Int status = client.exe cutemethod (get );
// Obtain the page content you want
Content = Get. getresponsebodyasstring ();
System. Out. println ("geturl:" + status );
System. Out. println (content );
} Catch (httpexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally
{
Get. releasec