Problem Statement:
Despite using correct login credentials, the Jsoup code posted below fails to log into the target website, displaying the login page's HTML code instead.
<code class="java">public void connect() { try { Connection.Response loginForm = Jsoup.connect("https://www.capitaliq.com/CIQDotNet/Login.aspx/login.php") .method(Connection.Method.GET) .execute(); org.jsoup.nodes.Document document = Jsoup.connect("https://www.capitaliq.com/CIQDotNet/Login.aspx/authentication.php") .data("cookieexists", "false") .data("username", "myUsername") .data("password", "myPassword") .cookies(loginForm.cookies()) .post(); System.out.println(document); } catch (IOException ex) { Logger.getLogger(WebCrawler.class.getName()).log(Level.SEVERE, null, ex); } }</code>
Solution:
In addition to the username, password, and cookies, the target website requires two additional values for login: VIEWSTATE and EVENTVALIDATION. To obtain these values:
<code class="java">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");</code>
Then, add these values to the post request (order doesn't matter):
<code class="java">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();</code>
To enable the "Remember Me" feature, add:
<code class="java">.data("myLogin$myEnableAutoLogin", "on")</code>
With these modifications, the login should now succeed as expected.
The above is the detailed content of How to Resolve Jsoup Login Form Submission Issues Due to Missing VIEWSTATE and EVENTVALIDATION Parameters?. For more information, please follow other related articles on the PHP Chinese website!