使用java中的httpclient登录Hotmail帐户

时间:2012-03-14 15:04:54

标签: java httpclient http-post hotmail

我想在java中使用httpclient登录到hotmail帐户。以下是我的代码。请注意,我找到了一个使用post方法传递的参数列表,并在firefox中添加了“篡改数据”。似乎hotmail使用javascript,因此我无法获取要使用post方法传递的少量参数的值。有人可以帮帮我吗?它带我到同一页面。和post方法返回的状态代码是200 OK,这意味着它不会重定向。

我真的很感激任何帮助!

感谢。

Ragini

public void executeParser(String website_url, String emailid,String password) throws Exception
{
    String ppft=null;
    client.getParams().setParameter(HttpMethodParams.USER_AGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/8.0 (.NET CLR 3.5.30729)");

    client.getParams().setCookiePolicy(org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);

    GetMethod get_siteurl = new GetMethod(website_url);
    client.executeMethod(get_siteurl);
    is = get_siteurl.getResponseBodyAsStream();
    output = StringFormat.convertStreamToString(is); 
    //System.out.println(output);
    get_siteurl.releaseConnection();

    Parser parser= new Parser(output);
    NodeList nodelist1 = parser.parse(null);
    NodeList list1 = nodelist1.extractAllNodesThatMatch(inputfilter , true);
    for(int i=0;i<list1.size();i++)
    {
        Node n=list1.elementAt(i);
        if(n.getText().contains("PPFT"))
        {
          ppft=n.getText();
          String s[]=ppft.split("value=");
          ppft=s[1];
          ppft=ppft.replaceAll("\"","");
          ppft=ppft.replaceAll("/","");
          //System.out.println(ppft);
          break;
        }

    }

    //POST PARAMETERS TO LOGIN TO HOTMAIL ACCOUNT
    PostMethod post_uname_pwd = new PostMethod("https://login.live.com/ppsecure/post.srf");

    post_uname_pwd.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    NameValuePair userid =   new NameValuePair("login",emailid);
    NameValuePair pwd = new NameValuePair("passwd", password);
    NameValuePair type = new NameValuePair("type","11" );
    NameValuePair loginoptions = new NameValuePair("LoginOptions", "3");
    NameValuePair newuser = new NameValuePair("NewUser","1");
    NameValuePair mest = new NameValuePair("MEST", "");
    NameValuePair idsbho = new NameValuePair("idsbho", "1");
    NameValuePair pwdpad = new NameValuePair("PwdPad", "");
    NameValuePair PPFT = new NameValuePair("PPFT",ppft);
    NameValuePair sso = new NameValuePair("sso", "");
    NameValuePair i1 = new NameValuePair("i1", "");
    NameValuePair i2 = new NameValuePair("i2", "1");
    NameValuePair i4 = new NameValuePair("i4","");
    NameValuePair i12 = new NameValuePair("i12","1");
    NameValuePair i13 = new NameValuePair("i13","");
    NameValuePair i17 = new NameValuePair("i17", "");
    NameValuePair button = new NameValuePair("SI", "Sign in");

    NameValuePair[] data = {userid,pwd,type,loginoptions,newuser,mest,idsbho,pwdpad,PPFT,sso,i1,i2,i4,i12,i13,i17,button};

    //post_uname_pwd.getParams().setCookiePolicy(org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);
    post_uname_pwd.setRequestBody(data);
    client.executeMethod(post_uname_pwd);

    is = post_uname_pwd.getResponseBodyAsStream();
    output = StringFormat.convertStreamToString(is); 
    System.out.println(output);
    //post_uname_pwd.releaseConnection();

    int statuscode = post_uname_pwd.getStatusCode();
    post_uname_pwd.releaseConnection();
    System.out.println(statuscode);
    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
            (statuscode == HttpStatus.SC_SEE_OTHER) ||
            (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) 
    {
        Header header = post_uname_pwd.getResponseHeader("Location");
        System.out.println("Header is : " + header.getValue());

        if (header != null) 
        {
            String newuri = header.getValue();

            //System.out.println("Redirect target after Post: " + newuri);
            GetMethod redirect = new GetMethod(newuri);
            client.executeMethod(redirect);
            is = redirect.getResponseBodyAsStream();
            output = StringFormat.convertStreamToString(is); 
            System.out.println(output);
            redirect.releaseConnection();
        }
    }

}

2 个答案:

答案 0 :(得分:2)

使用HtmlUnit,这是实现目标的最佳方式。

您可以从http://htmlunit.sourceforge.net/

获取

答案 1 :(得分:1)

我发现登录hotmail帐户只需要几个参数。这是我的最终代码:

public void executeParser(String website_url, String emailid,String password) throws Exception
{
    //Website url is https://login.live.com/ppsecure/post.srf
    String ppft=null;
    client.getParams().setParameter(HttpMethodParams.USER_AGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/8.0 (.NET CLR 3.5.30729)");

    client.getParams().setCookiePolicy(org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);

    GetMethod get_siteurl = new GetMethod(website_url);
    client.executeMethod(get_siteurl);
    is = get_siteurl.getResponseBodyAsStream();
    output = StringFormat.convertStreamToString(is); 
    get_siteurl.releaseConnection();

    Parser parser= new Parser(output);
    NodeList nodelist1 = parser.parse(null);
    NodeList list1 = nodelist1.extractAllNodesThatMatch(inputfilter , true);
    for(int i=0;i<list1.size();i++)
    {
        Node n=list1.elementAt(i);
        if(n.getText().contains("PPFT"))
        {
          ppft=n.getText();
          String s[]=ppft.split("value=");
          ppft=s[1];
          ppft=ppft.replaceAll("\"","");
          ppft=ppft.replaceAll("/","");
          break;
        }

    }

    //POST PARAMETERS TO LOGIN TO HOTMAIL ACCOUNT
    PostMethod postdata = new PostMethod("https://login.live.com/ppsecure/post.srf");

    postdata.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    NameValuePair userid =   new NameValuePair("login",emailid);
    NameValuePair pwd = new NameValuePair("passwd", password);
    NameValuePair type = new NameValuePair("type","11" );
    NameValuePair loginoptions = new NameValuePair("LoginOptions", "3");
    NameValuePair newuser = new NameValuePair("NewUser","1");
    NameValuePair PPFT = new NameValuePair("PPFT",ppft);        
    //NameValuePair button = new NameValuePair("SI", "Sign in");

    NameValuePair[] data = {userid,pwd,type,loginoptions,newuser,PPFT};

    postdata.setRequestBody(data);
    client.executeMethod(postdata);

    is =postdata.getResponseBodyAsStream();
    output = StringFormat.convertStreamToString(is); 
    postdata.releaseConnection();

    int statuscode = postdata.getStatusCode();
    postdata.releaseConnection();

    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
            (statuscode == HttpStatus.SC_SEE_OTHER) ||
            (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) 
    {
        Header header = postdata.getResponseHeader("Location");
        if (header != null) 
        {
            String newuri = header.getValue();
            GetMethod redirect = new GetMethod(newuri);
            client.executeMethod(redirect);
            redirect.releaseConnection();
        }
    }

    GetMethod get = new GetMethod("http://mail.live.com/default.aspx");
    get.setFollowRedirects(true);
    client.executeMethod(get);
    is = get.getResponseBodyAsStream();
    output = StringFormat.convertStreamToString(is); 
    System.out.println("This is the final page after login: "+ output);

}

}