2017-10-20 157 views
1

我嘗試登錄本網站使用Jsoup上:https://www.startus.cc/Jsoup認證

我用下面的代碼是:

String url = "https://www.startus.cc/user/login"; 
      String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36"; 

      Connection.Response response = Jsoup.connect(url).userAgent(userAgent) 
        .method(Connection.Method.GET) 
        .execute(); 

      response = Jsoup.connect(url) 
        .cookies(response.cookies()) 
        .data("name", "myname") 
        .data("pass", "mypass") 
        .data("op", "Log in") 
        .userAgent(userAgent) 
        .method(Connection.Method.POST) 
        .followRedirects(true) 
        .execute(); 

Document doc = Jsoup.connect("https://www.startus.cc/") 
       .cookies(response.cookies()) 
       .userAgent(userAgent) 
       .get(); 

     System.out.println(doc); 

它打印我頁面的HTML內容,但它仍未登錄。你知道這裏缺少什麼嗎?謝謝!

回答

2

您的POST請求中缺少一個參數 - form_build_id。請按照以下步驟登錄:

  1. 發送GET要求 - response = Jsoup.connect...主要頁:https://www.startus.cc/,並從響應中提取的form_build_id值。
  2. 發送POST請求https://www.startus.cc/user/login包括form_build_id第1步中得到了餅乾 - .cookies(response.cookies())
+0

謝謝你的幫助!你能解釋一下更多的form_build_id參數嗎? –

+0

似乎是表單狀態ID - 來源:https://drupal.stackexchange.com/a/203027 – r3dst0rm