Kamil
Kamil

Reputation: 1538

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

I know there's a lot of info out there but I couldn't find anything fitting to my problem. I want to gather data from a page I need to be logged in. Here is what I'm trying to do:

I gather Cookies doing:

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

And then read the html for the 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 login 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 got is the login page. I'm afraid those hidden values can be changed when I try:

Document doc2 = Jsoup.connect

Am I doing it right?

Upvotes: 4

Views: 4064

Answers (1)

Gricha
Gricha

Reputation: 1032

It's kind of mixed context when you set method to POST and then call GET request. Try this:

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

Upvotes: 6

Related Questions