How to connect to the site and get data using java jsoup?

I know that there is a lot of information, but I could not find anything suitable for my problem. I want to collect data from the page I need to log in to. Here is what I am trying to do:

I collect cookies:

Connection.Response res = Jsoup .connect("http://website.com/login?event=doLogin") .execute(); Map <String,String> cookies = res.cookies(); 

And then read the html for hidden values:

  Document doc = Jsoup .connect("http://website.com/login?event=doLogin") .cookies(cookies) .get(); html = doc.toString(); length = html.length(); counter = 0; for (int i = 0; i < length; i++) { if (html.startsWith("document.write", i)){ name[counter] = html.substring(i + 41, i + 144); value[counter] = "Login"; counter++; } if (html.startsWith("hidden", i)) { name[counter] = html.substring(i + 13, i + 81); value[counter] = html.substring(i + 90, i + 123); counter++; } } 

Finally, I want to use this information to log in using Cookies and hidden values:

  Document doc2 = Jsoup .connect("http://website.com/login?event=doLogin") .cookies(cookies) .data("email", " my@email ") .data("pass", "mypass") .data(name[0], value[0]) .data(name[1], value[1]) .data(name[2], value[2]) .method(Connection.Method.POST) .get(); System.out.println(doc2); 

But all I have is a login page. I am afraid that these hidden values ​​might change when I try:

 Document doc2 = Jsoup.connect 

Am I doing it right?

+4
source share
1 answer

This is a mixed context when you set the POST method and then call the GET request. Try the following:

 Connection.Response res = Jsoup.connect("http://website.com/login?event=doLogin") .execute(); ... Document doc = Jsoup.connect("http://website.com/login?event=doLogin") .cookies(res.cookies()) .data("email", " my@email ") .data("pass", "mypass") .data(name[0], value[0]) .data(name[1], value[1]) .data(name[2], value[2]) .post(); 
+6
source

All Articles