jsoup请求头添加cookie,jsoup发布和cookie

本文档介绍如何使用Jsoup库登录网站并获取其他页面内容。在登录成功后,需要获取并设置session ID cookie以便于后续请求维持会话。首先通过Jsoup的post方法登录,然后解析响应获取cookie。之后在请求其他页面时,利用cookie保持会话状态。
摘要由CSDN通过智能技术生成

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

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

解决方案

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值