Cookie使用Jsoup登录? [英] Cookie to log in with Jsoup?

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

问题描述

对于一个项目,我试图从网站获得数据只有在您从网站Goodreads.com登录时才可以访问。我是Jsoup的新人,因为我只使用它为这个特定的项目。从网站获取相关数据不是一个问题,但我似乎无法到达我需要的特定页面。我尝试访问的页面只有在登录时才可以查看,当不登录时,它会重定向到登录页面。



我浏览过答案



我现在的状况:

  String url =http://www.goodreads.com/friend/user/7493379-judith; 
Connection.Response res = Jsoup.connect(http://www.goodreads.com/user/sign_in)
.data(email,MYEMAIL,user_password,MYPASSWORD )
.method(Connection.Method.POST)
.execute();

文档doc2 = res.parse();
String sessionId = res.cookie(_ session_id);


文档doc = Jsoup.connect(url)
.cookie(_ session_id,sessionId)
.get();

我在这里的答案帮我解决了,但它不工作,我仍然只获取来自登录页面的数据重定向到。
我有几个问题:


  1. 最重要的是,

  2. 这里给出的答案是heve used method。(Method.POST)而不是方法(Connection.Method.POST)。当我使用第一个但是,我得到一个错误,该方法无法解决。任何人都知道为什么是这样的。

  3. 我看到的例子在.data()中使用了username和password。这些是指什么?我现在使用输入框的名称。它是名称,类型,id,是什么?由于Goodreads不是以用户名登录,而是作为电子邮件,我假设我必须更改它们。 (用户名和密码也不起作用)

  4. 示例还使用 http: //example.com/login.php 作为示例网址。 Goodreads没有/login.php页面。

  5. _session_id是Goodreads上的相关Cookie的名称。

  6. ol>

    如果任何人都能指出正确的方向,我将非常感激。

    解决方案


    1. Goodreads在登录时需要两件事情:首先,会话ID存储在cookie中,其次,您有一个随机生成的号码。您可以在首次访问登录页面而无需登录时获取这些信息:它将设置一个具有会话ID的Cookie,并且该窗体将包含名称为n的隐藏输入表单(即),并输入一个数字。保存这些并在登录时将它们分别作为Cookie和表单值传递。

    有关我发现此问题的一些备注:



    您需要意识到的第一件事是,您试图重新创建与浏览器使用Jsoup完全相同的请求。因此,为了检查您现在的工作是否正常工作,您可以尝试使用浏览器重新创建完全相同的情况。



    要重新创建代码,我去了到登录页面,然后我删除了所有我的Goodreads Cookie(因为您不发送任何cookie,当您发送登录请求以及),并尝试登录,只传递用户名和密码表单值。它给了一个错误,我的会话timd out。当我第一次加载登录页面,然后删除除了会话ID的所有cookie ,并没有删除n形式值,我可以成功登录。因此,您想要对登录页面进行一般的GET请求,检索您在那里获得的会话ID Cookie和隐藏的表单值,并将其与POST请求一起传递。


    1. 这可能是API改变了,或者有几种方法。在任何情况下,使用Connection.Method.POST都会很好。


    2. 是的,他们引用输入框的名称。这应该是id,但是,因为名称是在过去使用,并不是所有浏览器的所有版本支持传递ids作为数据,大多数网站只是添加两者。


    3. 如果您查看登录表单的源代码,可以看到form元素的method属性是确实是登录页面本身,所以这是发送请求的地方。


    PS。作为一个一般的提示,你可以使用Firefox扩展篡改数据删除表单数据甚至cookie(虽然有更容易的扩展)。


    For a project I'm trying to get data from a website only acessible when you're logged in from the site Goodreads.com. I'm new to Jsoup, since I'm using it only for this particular project. Getting the relevant data from the website is not a problem, but I can't seem to get to the particular page I need. The page I'm trying to acces is viewable only when logged in, when not logged in it rederects to the log-in page.

    I've looked through the answers here, but the answers given so far have not helped.

    What I have now:

    String url = "http://www.goodreads.com/friend/user/7493379-judith";
    Connection.Response res = Jsoup.connect("http://www.goodreads.com/user/sign_in")
                .data("email", "MYEMAIL", "user_password", "MYPASSWORD")
                .method(Connection.Method.POST)
                .execute();
    
            Document doc2 = res.parse();
            String sessionId = res.cookie("_session_id"); 
    
    
        Document doc = Jsoup.connect(url)
                .cookie("_session_id", sessionId)
                .get();
    

    I got this far with help of the answers here, but it doesn't work, I'm still only getting the data from the log-in page it rederects to. I have several questions:

    1. Most importantly of course; How can I make it work?
    2. The given answers here heve used method.(Method.POST) instead of method.(Connection.Method.POST) . When I use the first one however, I get an error that Method cannot be resolved. Anyone know why this is?
    3. The examples I've seen have used "username" and "password" in .data() . What exactly do these refer to? I've now used the name of the input box. Is it the name, the type, the id, what exactly? Since Goodreads does not refer to the log in as the username, but as the e-mail, I assume I have to change them. (username & password doesn't work either)
    4. Examples also use http://example.com/login.php as example url. Goodreads doesn't have a /login.php page though. Am I correct to assume I have to use the url with the log-in screen?
    5. _session_id is the name of the relevant cookie on Goodreads.

    I'd be very grateful if anyone can point me in the right direction!

    解决方案

    1. Goodreads requires two things when logging in: first, that you have a session ID stored in a cookie, and second, that you have a random generated number. You can get these when first visiting the login page without logging in: it will set a cookie with a session ID, and the form will contain a hidden input form (i.e. ) with the name "n" and value a number. Save these and pass them along as respectively a cookie and a form value when logging in.

    Some remarks about the way I found this out:

    The first thing you need to realise is that you're trying to recreate the exact same requests your browser does with Jsoup. So, in order to check whether what you have right now will work, you can try to recreate the exact same situation with your browser.

    To recreate your code, I went to the login page, then I deleted all my Goodreads cookies (as you don't send along any cookies when you send the login request as well), and attempted to sign in with only passing the username and password form values. It gave an error that my session had timd out. When I first loaded the login page and then deleted all cookies except the session ID and did not remove the "n" form value, I could log in successfully. Therefore, you want to make a general GET request to the sign in page first, retrieve the session ID cookie you get there and the hidden form value, and pass it along with the POST request.

    1. It could be that the API changed or that there just are several ways. Using Connection.Method.POST will do fine, in any case.

    2. Yes, they refer to the names of the input boxes. This should be id, however, since name was used in the past and not all versions of all browsers supported passing the ids as data, most websites are just adding both. Either should be fine.

    3. If you look at the source code of the sign in form, you can see that the "method" attribute of the form element is indeed the sign in page itself, so that's where it sends the request to.

    PS. As a general tip, you can use the Firefox extension "Tamper Data" to remove form data or even cookies (though there are easier extensions for that).

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

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