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.
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