apache HttpClient 访问 facebook

发布于 2024-08-14 21:54:31 字数 4803 浏览 6 评论 0原文

对于以下场景有什么示例、提示和指导吗?

我使用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;
}

Any examples, tips, guidance for the following scenario?

I have used Apache HttpClient to simulate the functionality of browser to access facebook through java application. to do that first i have to provide user credentials. i have used examples provided in the following web site.

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

But non of these methods works for facebook, following is the test code i have written for this purpose. i have not provided the all the methods written, only the method used to login to the facebook account is given here. relay appreciate any help

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲜血染红嫁衣 2024-08-21 21:54:31

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

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)。它们差异很大。

The following code, based on that sample, worked for me:

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

I am not sure if your code was managing properly cookies (and session id kept within one of them), maybe that was the problem. Hope this will help you.

Just to make clear version issue: I was using HttpClient version 4.X, not the old one (3.X). They differ significantly.

生寂 2024-08-21 21:54:31

也许您应该使用一个工具,例如 Selenium

Perhaps you should use a tool, such as Selenium

2024-08-21 21:54:31

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

Have you taken a look at HtmlUnit. It wraps the HttpClient to create a headless Java browser, with javaScript execution. This way you are not trying to hack the individual forms all the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文