Using Jsoup for POST Login Data

I am trying to enter this site: http://deeproute.com

This is my code.

Connection.Response res = null; Connection homeConnection = null; Document homePage = null; Map<String, String> loginCookies = null; try { res = Jsoup.connect("http://www.deeproute.com/") .data("cookieexists", "false") .data("name", user) .data("password", pswd).method(Method.POST) .execute(); } catch (IOException e) { e.printStackTrace(); } if (res != null) { loginCookies = res.cookies(); try { homePage = Jsoup.connect("http://www.deeproute.com") .cookies(loginCookies).get(); } catch (IOException e) { e.printStackTrace(); } 

Unfortunately, this simply returns the same page in a no-login state. What am I doing wrong?

+4
source share
2 answers

You need to read the form before publishing! You are missing the subbera = Login parameter.


 public static void main(String[] args) throws Exception { Connection.Response loginForm = Jsoup.connect("http://deeproute.com/deeproute/default.asp") .method(Connection.Method.GET) .execute(); Document document = Jsoup.connect("http://deeproute.com/deeproute/default.asp") .data("cookieexists", "false") .data("name", "username") .data("password", "pass") .data("subbera", "Login") .cookies(loginForm.cookies()) .post(); } 
+4
source

Maybe a few (some actually) months is too late, but I found that I had to take some breaks on the website where the site was designed to be really confusing (the site on which I needed to extract the data). You also had to log in first to get cookie information. @MariuszS answer was useful, but the only problem was that I was not sure what the expected Map / Key-Value-Pair pairs should have been. Thanks to Javascript, I quickly got the keys and values ​​of the form and was able to successfully log in. Here's the javascript:

 var str = ""; //the form selector ie //that which is to be submitted to. In //my case, i needed to submit the entire body var arr = $("input[name]"); for(var i = 0, l = arr.length; i<l; i++){ str+= '.data("'+ arr[i].name +'", "'+ arr[i].value +'")' } 

Add the value of "str" ​​to your Jsoup request before

 .method(Connection.Method.GET) .execute(); 

Hope this proves somewhat useful as it was for me :)

0
source

All Articles