JSoup登录网站并访问

时间:2016-10-02 07:38:01

标签: java php jsoup

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中使用它们。)

1 个答案:

答案 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!");          
}
相关问题