2012-03-14 5 views
0

javaでhttpclientを使用してhotmailアカウントにログインします。以下は私のコードです。私はpostメソッドで渡されるパラメータのリストを見つけました。Firefoxの "改ざんデータ"アドオンの助けを借りて。 Hotmailはjavascriptを使用しているようで、postメソッドで渡すべきいくつかのパラメータの値を取得できませんでした。誰でもここで私を助けてくれますか?私は同じページに連れて行っています。 postメソッドによって返されるステータスコードは200 OKです。つまり、リダイレクトされません。javaでhttpclientを使用してHotmailアカウントにログイン

本当に助けていただきありがとうございます。

ありがとうございました。

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(); 
     } 
    } 

} 
+0

Hotmailの?ああ、私は存在していても忘れていました...メモリレーンを降りてきてくれてありがとう。 – gnomed

+0

パラメータ "type"、 "LoginOptions"、 "NewUser"は実際にはオプションです。 –

答えて

2

使用HtmlUnit、それはあなたが望むものを達成するための最良の方法です。

あなたは、私が唯一のいくつかのパラメータは、アカウントをHotmailのためにログインするために必要であることが分かったhttp://htmlunit.sourceforge.net/

1

からそれを得ることができます。ここに私の最終的なコードです:

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); 

} 

}

関連する問題