JSoup 登录网站,然后打开另一个页面以保持登录 session 。
谁能解释一下如何登录 this website使用 JSoup?
我当前的代码(完全失败,我确信我走错了“轨道”):
Connection.Response loginForm = Jsoup.connect("http://www.rlo.nl/roostersinfoweb/infoweb/index.php")
.method(Connection.Method.GET)
.execute();
Document doc = Jsoup.connect("http://www.rlo.nl/roostersinfoweb/infoweb/index.php").get();Jsoup.connect("http://www.rlo.nl/roostersinfoweb/infoweb/authentication.php")
.data("cookieexists", "false")
.data("username", "110")
.data("password", "sgl16")
.data("login", "Login")
.cookies(loginForm.cookies())
.post();
我确信一件事:authentication.php 不存在。
我是 JSoup 的新手,所以请解释一些代码并告诉我有关“cookie”部分的信息(我知道 cookie 是什么,但不知道如何在 JSoup 中使用它们。)
最佳答案
有几点需要纠正:
- 登录端点不正确(正确的端点是
http://www.gymnasiumleiden.nl/roostersinfoweb/infoweb/index.php
); - 您发送的某些
POST
参数拼写错误(例如,username
应为user
); - 某些
POST
参数不是必需的(例如cookieexists
); - 您没有在
POST
请求中添加所需的csrf
参数。 - 您应该检查响应以了解登录是否正常。
我的工作代码(带有错误检查器):
Connection.Response loginForm = Jsoup
.connect("http://www.gymnasiumleiden.nl/roostersinfoweb/infoweb/index.php")
.method(Connection.Method.GET)
.execute();
Document doc = loginForm.parse();
String csrf = doc.select("input[name=csrf]").val();
Connection.Response response = Jsoup.connect("http://www.gymnasiumleiden.nl/roostersinfoweb/infoweb/index.php")
.data("user", "110638")
.data("paswoord", "sgl1617")
.data("login", "loginform")
.data("csrf", csrf)
.cookies(loginForm.cookies())
.method(Connection.Method.POST)
.execute();
String body = response.body();
if(body.contains("Wachtwoord is incorrect")){
System.out.println("Password incorrect!");
} else if(body.contains("Gebruikersnaam werd niet gevonden.")){
System.out.println("Not found username!");
} else {
System.out.println("Login successfully!");
}
关于java - 登录端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39814877/