Problems with login form with Jsoup

For some reason, this code will not allow me to enter the site when I use the correct registration information. System.out.println sends the login page code, indicating that my code is not working. Can someone tell me what I forget or what is wrong with him?

 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); } } 
+1
source share
1 answer

In addition to username , password and cookies , the site asks for two additional values ​​for login - VIEWSTATE and EVENTVALIDATION .
You can get them from the response of the first Get request, for example:

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

And add it after password (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(); 

I would also add a userAgent field for both requests - some sites check it and send different pages to different clients, so if you want the same response as in your browser, add .userAgent("Mozilla/5.0") to the requests (or any browser that you use).

Edit
The username field is myLogin$myUsername , the password is myLogin$myPassword , and the Post request also contains login button information. I can’t verify this because I do not have a user on this site, but I believe that it will work. Hope this solves your problem.

EDIT 2
To enable the remember me field during login, add this line to the Post request:

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

Source: https://habr.com/ru/post/1270563/


All Articles