使用JSoup登录页面的困难 [英] Difficulties using JSoup logging into page
问题描述
使用JSoup,我在登录页面时遇到了严重困难.受到在论坛上发现的启发,我进行了各种尝试,开始认为存在一个我不知道的基本错误.
Making use of JSoup, I am experiencing severe difficulties logging into a page. I have undertaken various attempts, inspired by what I found on the forum, that I start to think that there is a fundamental mistake I am not aware of.
Connection.Response loginForm = Jsoup.connect("https://*****.*****.com/login")
.method(Connection.Method.GET)
.execute();
Document res = Jsoup.connect("https://*****.*****.com/login")
//.data("appToken", "ef626867eae90b9f7bca8c861ef97abb370987da")
.data("appToken", "RandomlyGeneratingToken, see comment above")
.data("emailAddress", "*****@*****.***")
.data("password", "*****")
.userAgent("Mozilla")
.post();
这是否与随机生成的输入.data("appToken", ???)
有关,输入每次在请求站点时都会更改,因此我尤其不确定在此阶段要发布什么数据.执行.post();
时发生错误"HTTP错误获取URL状态= 400".任何帮助将不胜感激-非常感谢您!
Does this have something to do with the randomly generating input .data("appToken", ???)
, which changes everytime the site is being requested, which makes me particularly unsure about what data to post at this stage. Error 'HTTP error fetching URL Status=400' occurs when .post();
is being executed. Any help will be highly appreciated - thank you very much, in advance!
推荐答案
几件事:
-
GET
请求-像在POST
请求中一样添加user agent
字符串始终是一个好习惯:
The
GET
request - it's always a good practice to add theuser agent
string like you did at thePOST
request:
Connection.Response loginForm = Jsoup.connect("https://pro-labs.imdb.com/login")
.method(Connection.Method.GET)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0")
.execute();
随机的"appToken"-您必须从对上一个GET
请求的响应中将其提取-您不能使用随机或旧值:
The random "appToken" - you must extract it from the response to the previous GET
request - you cannot use random or old value:
Document doc = loginForm.parse();
Element e = doc.select("input[name=appToken]").first();
String appToken = e.attr("value");
POST
请求-您必须添加服务器发送给您的会话cookie以及对GET
请求的响应-
The POST
request - you must add the session cookies that the server sends you with the response to the GET
request -
Document res = Jsoup.connect("https://pro-labs.imdb.com/login")
.data("appToken", appToken)
.data("emailAddress", "*****@*****.***")
.data("password", "*****")
.userAgent("Mozilla")
.cookies(loginForm.cookies())
.post();
这篇关于使用JSoup登录页面的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!