」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何克服使用 Jsoup 提交登入表單時的身份驗證問題

如何克服使用 Jsoup 提交登入表單時的身份驗證問題

發佈於2024-11-07
瀏覽:967

How to Overcome Authentication Issues While Submitting Login Forms with Jsoup

使用Jsoup 提交登入表單時遇到的挑戰

使用Jsoup 提交登入表單時遇到的挑戰

儘管輸入了有效的登入憑證,但使用提供的程式碼造訪網站時可能會遇到困難。執行後,程式碼無法進行身份驗證,這表示存在潛在問題。

Document doc = loginForm.parse();
Element e = doc.select("input[id=__VIEWSTATE]").first();
String viewState = e.attr("value");
e = doc.select("input[id=__EVENTVALIDATION]").first();
String eventValidation = e.attr("value");
要解決此問題,請從初始 GET 請求的回應中檢索這些值:

org.jsoup.nodes.Document document = (org.jsoup.nodes.Document) Jsoup.connect("https://www.capitaliq.com/CIQDotNet/Login.aspx/authentication.php").userAgent("Mozilla/5.0")               
            .data("myLogin$myUsername", "MyUsername")
            .data("myLogin$myPassword, "MyPassword")
            .data("myLogin$myLoginButton.x", "22")                   
            .data("myLogin$myLoginButton.y", "8")
            .data("__VIEWSTATE", viewState)
            .data("__EVENTVALIDATION", eventValidation)
            .cookies(loginForm.cookies())
            .post();
將這些值合併到後續的POST 請求中:

org.jsoup.nodes.Document document = (org. jsoup.節點.Document) Jsoup.connect("https://www.capitaliq.com/CIQDotNet/Login.aspx/authentication.php").userAgent("Mozilla/5.0") .data("myLogin$myUsername", "我的使用者名稱") .data("myLogin$myPassword, "我的密碼") .data("myLogin$myLoginButton.x", "22") .data("myLogin$myLoginButton.y", "8") .data("__VIEWSTATE", viewState) .data("__EVENTVALIDATION", eventValidation) .cookies(loginForm.cookies()) .post();包含 userAgent 欄位以確保與網站特定於瀏覽器的渲染相容。

增強功能
.data("myLogin$myEnableAutoLogin", "on")
要啟動「記住登入期間的「我」功能:

How to Overcome Authentication Issues While Submitting Login Forms with Jsoup

.data("myLogin$myEnableAutoLogin" , "on")

版本聲明 本文轉載於:1729401436如有侵犯,請洽[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3