How to Resolve Jsoup Login Form Submission Issues Due to Missing VIEWSTATE and EVENTVALIDATION Parameters?

Mary-Kate Olsen
Release: 2024-10-20 13:17:30
Original
209 people have browsed it

How to Resolve Jsoup Login Form Submission Issues Due to Missing VIEWSTATE and EVENTVALIDATION Parameters?

Troubleshooting Jsoup Login Form Submission Issues

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>
Copy after login

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>
Copy after login

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>
Copy after login

To enable the "Remember Me" feature, add:

<code class="java">.data("myLogin$myEnableAutoLogin", "on")</code>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!