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

查看:32
本文介绍了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,需要在后续请求中发送该 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天全站免登陆