Paul Buciuman
Paul Buciuman

Reputation: 335

Jsoup Authentication

I'm trying to login on this website using Jsoup:https://www.startus.cc/

I'm using the following code for it:

String url = "https://www.startus.cc/user/login";
            String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36";

            Connection.Response response = Jsoup.connect(url).userAgent(userAgent)
                    .method(Connection.Method.GET)
                    .execute();

            response = Jsoup.connect(url)
                    .cookies(response.cookies())
                    .data("name", "myname")
                    .data("pass", "mypass")
                    .data("op", "Log in")
                    .userAgent(userAgent)
                    .method(Connection.Method.POST)
                    .followRedirects(true)
                    .execute();   

Document doc = Jsoup.connect("https://www.startus.cc/")
                .cookies(response.cookies())
                .userAgent(userAgent)
                .get();

        System.out.println(doc);

It does print me the html content of the page, but it is still not logged in. Do you know what is missing here? Thank you!

Upvotes: 1

Views: 845

Answers (1)

TDG
TDG

Reputation: 6151

There is a missing parameter in your POST request - form_build_id. Follow these steps to login:

  1. Send GET request - response = Jsoup.connect... to the main page: https://www.startus.cc/ and extract the value of form_build_id from the response.
  2. Send POST request to https://www.startus.cc/user/login that includes form_build_id and the cookies you got from step 1 - .cookies(response.cookies()).

Upvotes: 2

Related Questions