jsoup发布和cookie [英] jsoup posting and cookie

查看:247
本文介绍了jsoup发布和cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jsoup登录网站,然后抓取信息,我遇到问题,我可以成功登录并从index.php创建一个文档,但我无法在网站上获取其他页面。我知道我需要在发布后设置一个cookie,然后在我尝试在网站上打开另一个页面时加载它。但是我该怎么做?以下代码让我登录并获取index.php

I'm trying to use jsoup to login to a site and then scrape information, I am running into in a problem, I can login successfully and create a Document from index.php but I cannot get other pages on the site. I know I need to set a cookie after I post and then load it when I'm trying to open another page on the site. But how do I do this? The following code lets me login and get index.php

Document doc = Jsoup.connect("http://www.example.com/login.php")
               .data("username", "myUsername", 
                     "password", "myPassword")
               .post();

我知道我可以使用apache httpclient来做到这一点,但我不想这样做。

I know I can use apache httpclient to do this but I don't want to.

推荐答案

当您登录该站点时,可能会设置一个授权会话cookie,需要在后续维护请求时发送会话。

When you login to the site, it is probably setting an authorised session cookie that needs to be sent on subsequent requests to maintain the session.

您可以像这样获得cookie:

You can get the cookie like this:

Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
    .data("username", "myUsername", "password", "myPassword")
    .method(Method.POST)
    .execute();

Document doc = res.parse();
String sessionId = res.cookie("SESSIONID"); // you will need to check what the right cookie name is

然后在下一个请求中发送它喜欢:

And then send it on the next request like:

Document doc2 = Jsoup.connect("http://www.example.com/otherPage")
    .cookie("SESSIONID", sessionId)
    .get();

这篇关于jsoup发布和cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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