3

以下场景的任何示例、提示和指导?

我使用 Apache HttpClient 来模拟浏览器通过 java 应用程序访问 facebook 的功能。首先,我必须提供用户凭据。我使用了以下网站中提供的示例。

http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/

但这些方法都不适用于 facebook,以下是我为此目的编写的测试代码。我没有提供所有编写的方法,这里只给出了用于登录 facebook 帐户的方法。中继感谢任何帮助

    private static int connectAndLogin(String email, String pass){
    logger.trace("Facebook: =========connectAndLogin begin===========");

    String httpResponseBody = getMethod("http://www.facebook.com/login.php");
    if(httpResponseBody == null){
        //Why don't we try again?
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            logger.trace(e.getMessage());
        }
        httpResponseBody = getMethod("http://www.facebook.com/login.php");
    }
    logger.trace("Facebook: ========= get login page ResponseBody begin===========");
    logger.trace(httpResponseBody);
    logger.trace("Facebook: +++++++++ get login page ResponseBody end+++++++++");

    logger.trace("Facebook: Initial cookies: ");
    List<Cookie> cookies = getCookies();
    if (cookies.isEmpty()) {
        logger.trace("Facebook: None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            logger.trace("Facebook: - " + cookies.get(i).toString());
        }
    }
    if(httpResponseBody == null){
        logger.warn("Facebook: Warning: Failed to get facebook login page.");
    }

    try
    {
        HttpPost httpost = new HttpPost("http://www.facebook.com/login.php");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("email", email));
        nvps.add(new BasicNameValuePair("pass", pass));
        //don't know if is this necessary
        nvps.add(new BasicNameValuePair("login", ""));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        logger.info("Facebook: @executing post method to:" + "http://www.facebook.com/login.php");

        HttpResponse loginPostResponse = getHttpClient().execute(httpost);
        HttpEntity entity = loginPostResponse.getEntity();

        logger.trace("Facebook: Login form post: " + loginPostResponse.getStatusLine());
        if (entity != null) {
            logger.trace("Facebook: "+EntityUtils.toString(entity));
            entity.consumeContent();
        } else {
            logger.error("Facebook: Error: login post's response entity is null");
            return FacebookErrorCode.kError_Login_GenericError;
        }

        logger.trace("Facebook: Post logon cookies:");
        cookies = getCookies();
        if (cookies.isEmpty()) {
            logger.trace("Facebook: None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                logger.trace("Facebook: - " + cookies.get(i).toString());
            }
        }

        int statusCode = loginPostResponse.getStatusLine().getStatusCode();

        logger.info("Facebook: Post Method done(" + statusCode + ")");



        switch(statusCode){
        case 100: break;//we should try again;
        case 301:
        case 302:
        case 303:
        case 307:
        {
            //redirect
    //                Header[] headers = loginPostResponse.getAllHeaders();
    //                for (int i=0; i<headers.length; i++) {
    //                    logger.trace("Facebook: "+headers[i]);
    //                }
     //                Header locationHeader =         loginPostResponse.getFirstHeader("location");
    //                if(locationHeader != null){
    //                    homePageUrl = locationHeader.getValue();
    //                    logger.info("Facebook: Redirect Location: " + homePageUrl);
    //                    if(homePageUrl == null 
    //                        || !homePageUrl.contains("facebook.com/home.php")){
    //                        logger.error("Facebook: Login error! Redirect Location Url not contains \"facebook.com/home.php\"");
    //                        return FacebookErrorCode.kError_Login_GenericError;
    //                    }
    //                } else {
    //                    logger.warn("Facebook: Warning: Got no redirect location.");
    //                }
        }
        break;
        default:;
        }
    }
    catch (IOException ioe)
    {
        logger.error("Facebook: IOException\n" + ioe.getMessage());
        return FacebookErrorCode.kError_Global_ValidationError;
    }

    logger.trace("Facebook: =========connectAndLogin end==========");
    return FacebookErrorCode.Error_Global_NoError;
}
4

3 回答 3

2

以下基于该示例的代码对我有用:

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet("http://www.facebook.com/login.php");

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
    entity.consumeContent();
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
    System.out.println("None");
} else {
    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("- " + cookies.get(i).toString());
    }
}

HttpPost httpost = new HttpPost("http://www.facebook.com/login.php");

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email", "******"));
nvps.add(new BasicNameValuePair("pass", "*******"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Double check we've got right page " + EntityUtils.toString(entity));

System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
    entity.consumeContent();
}

System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
    System.out.println("None");
} else {
    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("- " + cookies.get(i).toString());
    }
}

httpclient.getConnectionManager().shutdown();  

我不确定您的代码是否正确管理 cookie(并且会话 ID 保存在其中一个中),也许这就是问题所在。希望这会帮助你。

只是为了明确版本问题:我使用的是 HttpClient 版本 4.X,而不是旧版本(3.X)。它们显着不同。

于 2009-12-15T14:25:00.357 回答
0

也许你应该使用一个工具,比如Selenium

于 2009-12-14T16:46:46.763 回答
0

你有没有看过HtmlUnit。它包装了 HttpClient 以创建一个无头 Java 浏览器,并执行 javaScript。这样,您就不会一直试图破解单个表单。

于 2009-12-14T17:13:49.017 回答