Automatically log on to java and obtain data programs

Source: Internet
Author: User

For 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 = "jsp course"> http://reg.163.com/login.jsp ";
   
Private String CookieStr;
Private Date EndTime;
   
Public void autoLogin (){
HttpClient 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 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

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.