2016-10-02 126 views
0

JSoup登錄網站,然後打開另一個頁面保持登錄會話。JSoup登錄網站並訪問

任何人都可以解釋如何使用JSoup登錄this website

用戶名:

任何(EG 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,所以請解釋一些代碼,並告訴我的「餅乾」部分(我知道cookies是什麼,但不知道如何使用它們與JSoup。)

回答

4

有一些點正確的:

  • 登錄終結點不正確(正確的是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!");   
} 
+0

謝謝!你能向我解釋你是如何發現的嗎? –

+3

[https://www.mkyong.com/java/how-to-automate-login-a-website-java-example/](https://www.mkyong.com/java/how-to-automate- login-a-website-java-example /)和[http://www.javacodeexamples.com/jsoup-login-to-website-using-post-method-example/830](http://www.javacodeexamples。 com/jsoup-login-to-website-using-post-method-example/830)漂亮的教程,但是@davide也可以添加他自己的建議 –

+0

@Rishaldevsingh謝謝! –