2012-03-14 5 views
0

java에서 httpclient로 hotmail 계정에 로그인하고 싶습니다. 다음은 내 코드입니다. 파이어 폭스에 "탬퍼 데이터"추가 기능을 사용하여 post 메서드와 함께 전달할 매개 변수 목록을 찾았습니다. 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

핫메일! 오, 세상에, 심지어 그 존재를 잊어 버렸습니다 ... 추억의 차선에서 내려 주셔서 감사합니다. – gnomed

+0

"type", "LoginOptions", "NewUser"매개 변수는 실제로 선택 사항입니다. –

답변

2

사용 HtmlUnit과, 그것은 당신이 원하는 것을 달성하는 가장 좋은 방법입니다.

당신은 내가 단지 몇 가지 매개 변수가 계정을 핫메일에 로그인하는 데 필요한 것을 발견 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); 

} 

}

관련 문제