使用httpclient登录yahoo.de

时间:2011-06-10 14:02:09

标签: javascript httpclient

我正在尝试使用htpclient登录yahoo.de。但我无法这样做。从输出我看到它需要javascript来处理。有谁知道我该怎么做? 这是我的程序的代码和输出..我真的很感激任何帮助.. 感谢。

import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;

import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; 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; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.http.Header; import org.htmlparser.Node; import org.htmlparser.NodeFilter; import org.htmlparser.Parser; import org.htmlparser.filters.TagNameFilter; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException;

公共类YahooParser {      HttpClient client = new HttpClient();      static NodeFilter inputfilter = new TagNameFilter(“input”);      static NodeFilter linkfilter = new TagNameFilter(“a”);     void parser_getemails(String website_url,String emailID,String password)抛出HttpException,IOException,ParserException     {     String url_post = 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 / 3.6.3(.NET CLR 3.5.30729)”);

GetMethod get_siteurl = new GetMethod(website_url);
//get_siteurl.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.executeMethod(get_siteurl);
String output_get=get_siteurl.getResponseBodyAsString();
//System.out.println(get_siteurl.getResponseBodyAsString());
get_siteurl.releaseConnection();

Parser parser= new Parser(output_get);
NodeList nodelist1 = parser.parse(null);
NodeList list1 = nodelist1.extractAllNodesThatMatch(inputfilter , true);
String u=list1.elementAt(10).getText();
String challenge=list1.elementAt(12).getText();

//System.out.println("u is "+u); System.out.println("chllange is: "+challenge);
String s[]=u.split("\""); u=s[5];
String s1[]=challenge.split("\"");challenge=s1[5];

PostMethod post_uname_pwd = new PostMethod("https://login.yahoo.com/config/login?");
post_uname_pwd.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

NameValuePair userid =   new NameValuePair("login","emailid@yahoo.de");
NameValuePair pwd = new NameValuePair("passwd", "password");
NameValuePair tries =  new NameValuePair(".tries", "1");
NameValuePair src =  new NameValuePair(".src", "ym");
NameValuePair md5 =  new NameValuePair(".md5", "");
NameValuePair hash =  new NameValuePair(".hash", "");
NameValuePair js =  new NameValuePair(".js", "");
NameValuePair last =  new NameValuePair(".last", "");
NameValuePair promo =  new NameValuePair("promo", "");
NameValuePair intl =  new NameValuePair(".intl", "de");
NameValuePair bypass =  new NameValuePair(".bypass", "");
NameValuePair partner =  new NameValuePair(".partner", "");
NameValuePair u1 =  new NameValuePair(".u", u);
NameValuePair v =  new NameValuePair(".v", "0");
NameValuePair challenge1 =  new NameValuePair(".challenge", challenge);
NameValuePair yplus =  new NameValuePair(".yplus", "");
NameValuePair emailcode =  new NameValuePair(".emailCode", "");
NameValuePair pkg =  new NameValuePair("pkg", "c");
NameValuePair stepid =  new NameValuePair("stepid", "");
NameValuePair ev =  new NameValuePair(".ev", "");
NameValuePair hasmsgr =  new NameValuePair("hasMsgr", "0");
NameValuePair chkp =  new NameValuePair(".chkP", "Y");
NameValuePair  done=  new NameValuePair(".done", "http://mail.yahoo.com");
NameValuePair pd =  new NameValuePair(".pd", "ym_ver=0&c=&ivt=&sg=");
NameValuePair[] data = {userid,pwd,tries,src,md5,hash,js,last,promo,intl,bypass,partner,u1,v,challenge1,yplus,emailcode,pkg,stepid,ev,hasmsgr,chkp,done,pd};

post_uname_pwd.getParams().setCookiePolicy(org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);
post_uname_pwd.setRequestBody(data);
post_uname_pwd.setRequestHeader("user-agent", "Mozilla/5.0 (Macintosh; U; PPCMac OS X; en-us) AppleWebKit/124 (KHTML, like Gecko) Safari/125.1");
post_uname_pwd.addRequestHeader("Accept", "*/*"); 
post_uname_pwd.addRequestHeader("Accept-Language", "en-us, ja;q=0.21,de-de;q=0.86, de;q=0.79, fr-fr;q=0.71, fr;q=0.64, nl-nl;q=0.57,nl;q=0.50, it-it;q=0.43, it;q=0.36, ja-jp;q=0.29, en;q=0.93,es-es;q=0.14, es;q=0.07");
client.executeMethod(post_uname_pwd);
String post_op=post_uname_pwd.getResponseBodyAsString();
System.out.println(post_op);
int statuscode = post_uname_pwd.getStatusCode();
System.out.println(statuscode);
post_uname_pwd.releaseConnection();


if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
        (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
        (statuscode == HttpStatus.SC_SEE_OTHER) ||
        (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) 
{
    Header header = (Header) post_uname_pwd.getResponseHeader("Location");
    if (header != null) 
    {
        url_post = header.getValue();
        System.out.println(url_post);
        GetMethod redirect = new GetMethod(url_post);
        client.executeMethod(redirect);
        String output =redirect.getResponseBodyAsString();
        //System.out.println(output);
        if(output != null)
        {
            //System.out.println(output);
            FileOutputStream fos = new FileOutputStream("C:\\Documents and Settings\\patel\\My Documents\\yahoo.html");
            DataOutputStream dos = new DataOutputStream(fos);
            dos.writeBytes(output); 
            File file = new File("C:\\Documents and Settings\\patel\\My Documents\\yahoo.html");
            FileInputStream fis = new FileInputStream(file);

        }       
        redirect.releaseConnection();
    }
}
}

}

输出:

如果您看到此页面,则您的浏览器设置会阻止您 从自动重定向到新的URL。

click here继续。

2 个答案:

答案 0 :(得分:0)

HttpClient无法处理JavaScript。您需要了解yahoo.de是否有可以使用的非JavaScript替代方案。

答案 1 :(得分:0)

最后,我可以使用非雅虎版的雅虎登录yahoo.de。这是代码。

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;


public class YahooParser 
{
 HttpClient client = new HttpClient();
 static NodeFilter inputfilter= new TagNameFilter("input");
 static NodeFilter linkfilter = new TagNameFilter("a");
void parser_getemails(String website_url, String emailID, String password)   throws    HttpException, IOException, 

ParserException, InterruptedException
{
String redirect_url=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/3.6.3 (.NET CLR 3.5.30729)");

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

GetMethod get_siteurl = new GetMethod("http://de.mc1612.mail.yahoo.com/mc/welcome?ymv=0");
//get_siteurl.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.executeMethod(get_siteurl);
String output_get=get_siteurl.getResponseBodyAsString();
//System.out.println(get_siteurl.getResponseBodyAsString());
get_siteurl.releaseConnection();

Parser parser= new Parser(output_get);
NodeList nodelist1 = parser.parse(null);
NodeList list1 = nodelist1.extractAllNodesThatMatch(inputfilter , true);
String u=list1.elementAt(10).getText();
String challenge=list1.elementAt(12).getText();

//System.out.println("u is "+u); System.out.println("challange is: "+challenge);
String s[]=u.split("\""); u=s[5];
String s1[]=challenge.split("\"");challenge=s1[5];

PostMethod post_uname_pwd = new PostMethod("https://login.yahoo.com/config/login?");
post_uname_pwd.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

NameValuePair userid =   new NameValuePair("login","emailid");
NameValuePair pwd = new NameValuePair("passwd", "pwd");
NameValuePair tries =  new NameValuePair(".tries", "1");
NameValuePair src =  new NameValuePair(".src", "ym");
NameValuePair md5 =  new NameValuePair(".md5", "");
NameValuePair hash =  new NameValuePair(".hash", "");
NameValuePair js =  new NameValuePair(".js", "");
NameValuePair last =  new NameValuePair(".last", "");
NameValuePair promo =  new NameValuePair("promo", "");
NameValuePair intl =  new NameValuePair(".intl", "de");
NameValuePair bypass =  new NameValuePair(".bypass", "");
NameValuePair partner =  new NameValuePair(".partner", "");
NameValuePair u1 =  new NameValuePair(".u", u);
NameValuePair v =  new NameValuePair(".v", "0");
NameValuePair challenge1 =  new NameValuePair(".challenge", challenge);
NameValuePair yplus =  new NameValuePair(".yplus", "");
NameValuePair emailcode =  new NameValuePair(".emailCode", "");
NameValuePair pkg =  new NameValuePair("pkg", "");
NameValuePair stepid =  new NameValuePair("stepid", "");
NameValuePair ev =  new NameValuePair(".ev", "");
NameValuePair hasmsgr =  new NameValuePair("hasMsgr", "0");
NameValuePair chkp =  new NameValuePair(".chkP", "Y");
NameValuePair  done=  new NameValuePair(".done",   "http://de.mc1612.mail.yahoo.com     /mc/welcome?ymv=0");
NameValuePair pd =  new NameValuePair(".pd", "ym_ver=0&c=&ivt=&sg=");
NameValuePair pad =  new NameValuePair("pad", "6");
NameValuePair add =  new NameValuePair("add", "6");
NameValuePair save=new NameValuePair(".save","");
NameValuePair persistent=new NameValuePair(".persistent","y");
NameValuePair[] data =   {userid,pwd,tries,src,md5,hash,js,last,promo,intl,bypass,partner,u1,v,challenge1,yplus,emailcode,pkg,stepid,ev,hasmsgr,chkp,done,pd

,pad,add,save,persistent};

   post_uname_pwd.getParams().setCookiePolicy(org.apache.http.client.params.CookiePolicy.BROWS       ER_COMPATIBILITY);
post_uname_pwd.setRequestBody(data);
client.executeMethod(post_uname_pwd);
int statuscode = post_uname_pwd.getStatusCode();
System.out.println("status code of post method: "+statuscode);
String post_op=post_uname_pwd.getResponseBodyAsString();
System.out.println("Output of post_uname_pwd"+post_op);
post_uname_pwd.releaseConnection();

parser = new Parser(post_op);
nodelist1 = parser.parse(null);
list1 = nodelist1.extractAllNodesThatMatch(linkfilter , true);
Node n = list1.elementAt(0);
String login_url=n.getText();
String st[]=login_url.split("\"");
login_url=st[1];
System.out.println("get login url is: "+login_url);

GetMethod get_loginurl = new GetMethod(login_url);
    get_loginurl.getParams().setCookiePolicy(org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);

client.executeMethod(get_loginurl);
String get_loginurl_op=get_loginurl.getResponseBodyAsString();
System.out.println("Output of get_login_url"+get_loginurl_op);
statuscode=get_loginurl.getStatusCode();
System.out.println("status code of get_loginurl method: "+ statuscode);

FileOutputStream fos= new FileOutputStream("C:\\Documents and Settings\\patel\\My Documents\\yahoologin.html");
DataOutputStream dos = new DataOutputStream(fos);
   dos.writeChars(get_loginurl_op); 


}

}