使用Jsoup和适当的Cookie登录到Facebook [英] Login to Facebook with Jsoup and proper cookies

查看:257
本文介绍了使用Jsoup和适当的Cookie登录到Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试自动剪贴簿我自己的主页以及登录Facebook时可能访问的其他页面.但是,在使用下面的代码并设置cookie之后,我似乎无法登录".

I am currently trying to automatically scrap my own home page and possibly other pages that I have access to when logged in to facebook. However I can't seem to be "logged" in after using the code below and setting the cookie.

Connection.Response res = Jsoup.connect("http://www.facebook.com/login.php?login_attempt=1")
            .data("email", "#####", "pass", "#####").userAgent("Mozilla")
            .method(Method.POST)
            .execute();

    Map<String, String> cookies = res.cookies();

        try{
            Document doc2 = Jsoup.connect("https://www.facebook.com/")
                .cookies(cookies).post();
            System.out.println(doc2.text());
            }
            catch(Exception e){
                e.printStackTrace();
            }

当我这样做时,它将像没有登录时一样将我发回Facebook主页.当我尝试自己的About页面时,我会得到

When I do this it will just send me back the facebook home page as though I were not logged in. When I try my own about page I get

HTTP error fetching URL. Status=404

我在这里做错什么了吗?我还需要设置其他字段吗? 我发现另一则帖子说需要设置其他字段,但是我丝毫不知道如何找到此信息.该帖子可以在这里找到通过Jsoup登录Facebook

Am I doing something wrong here? Are there other fields I need to set? I found another post that said other fields need to be set but I haven't the slightest clue how to find this information. The post can be found here Login Facebook via Jsoup

任何建议都会有所帮助.谢谢你.

Any advice would be helpful. Thank you in advanced.

推荐答案

您可以在此处查看示例:

You can see a example here: How to Login on Facebook w/ Jsoup

public static void main(String[] args) {

    Response req;
    try {
        String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36";

        req = Jsoup.connect("https://m.facebook.com/login/async/?refsrc=https%3A%2F%2Fm.facebook.com%2F&lwv=100")
            .userAgent(userAgent)
            .method(Method.POST).data("email", "YOUR_EMAIL").data("pass", "YOUR_PASSWORD")
            .followRedirects(true).execute();

        Document d = Jsoup.connect("https://m.facebook.com/profile.php?ref=bookmarks").userAgent(userAgent)
            .cookies(req.cookies()).get();

        System.out.println(d.body().text().toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

}

这篇关于使用Jsoup和适当的Cookie登录到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆