Java – Log in to a website with Jsoup and stay on it

Log in to a website with Jsoup and stay on it… here is a solution to the problem.

Log in to a website with Jsoup and stay on it

I tried logging in here: http://www.gszi.sulinet.hu/dinaweb/diakok/belepes.jsp
With this:

Connection.Response loginForm = Jsoup.connect("http://www.gszi.sulinet.hu/dinaweb/diakok/belepes.jsp")
            .method(Connection.Method.GET)
            .execute();

Document doc = Jsoup.connect("http://www.gszi.sulinet.hu/dinaweb/diakok/belepes.jsp")
            .data("name","myid")
            .data("name","mycode")
            .cookies(loginForm.cookies())
            .post();

After that, getting the html of the page I had to log in to see, I realized I couldn’t log in. Is there a way to log in and then get the html of the page I can access now? Any links, suggestions or help are appreciated.

Solution

You use the same key for two different input labels. Also, you are using the wrong key.

.data("jelszo","SOMETEXT")
.data("felnev","PASSWORD")

Update

Connection.Response initial = Jsoup
        .connect("http://www.gszi.sulinet.hu/dinaweb/diakok/belepes.jsp")
        .method(Connection.Method.GET).execute();

Connection.Response login = Jsoup
        .connect("http://www.gszi.sulinet.hu/dinaweb/diakok/belepes.jsp")
        .data("jelszo","SOMETEXT")
        .data("felnev","PASSWORD")
        .cookies(initial.cookies())
        .method(Method.POST)
        .execute();

Document page = Jsoup
        .connect("ANY_PAGE_INSIDE_THE_SITE")
        .cookies(login.cookies()) //use this with any page you parse. it will log you in
        .get();

Related Problems and Solutions