JSoup登录网站,然后打开另一个页面,保持该登录会话。
有人可以解释如何使用JSoup登录this website吗?
用户名:
任何事情(E.G。110000,110001和110002都有效)
密码:
" sgl1617" (我不在乎知道密码的人,你不能用它做任何事情。)
我的代码目前(完全失败,我确定我错了'轨道'):
Connection.Response loginForm = Jsoup.connect("http://www.gymnasiumleiden.nl/roostersinfoweb/infoweb/index.php")
.method(Connection.Method.GET)
.execute();
Document doc = Jsoup.connect("http://www.gymnasiumleiden.nl/roostersinfoweb/infoweb/index.php").get();Jsoup.connect("http://www.gymnasiumleiden.nl/roostersinfoweb/infoweb/authentication.php")
.data("cookieexists", "false")
.data("username", "110638")
.data("password", "sgl1617")
.data("login", "Login")
.cookies(loginForm.cookies())
.post();
我确信:authentication.php不存在。
我是JSoup的新手,所以请解释一些代码并告诉我有关cookie的信息。 part(我知道什么是cookie,但不知道你如何在JSoup中使用它们。)
答案 0 :(得分:4)
有一些要纠正的要点:
http://www.gymnasiumleiden.nl/roostersinfoweb/infoweb/index.php
); POST
参数拼写错误(例如username
应为user
); POST
参数(例如cookieexists
); csrf
请求中添加所需的POST
参数。我的工作代码(带错误检查程序):
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!");
}