"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Resolve Jsoup Login Form Submission Issues Due to Missing VIEWSTATE and EVENTVALIDATION Parameters?

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

Published on 2024-11-25
Browse:110

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.

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);
    }
}

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:

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");

Then, add these values to the post request (order doesn't matter):

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();

To enable the "Remember Me" feature, add:

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

With these modifications, the login should now succeed as expected.

Release Statement This article is reprinted at: 1729401319 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3