简体   繁体   中英

Login to a website using Jsoup and stay on the site

I try to log 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();

Afterwards, getting the html of a page which I have to login to see, I realize that I couldn't log in. Is there a way to login and then get the html of pages I have access now? Any link, advice or help appreciated.

You are using the same key for two different input tags. Moreover the keys you are using are wrong.

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

ok, actually I have found that I need one more parameter ("akcio") and I sent post() method to the wrong URL now my program runs properly, my code:

Connection.Response loginForm = Jsoup.connect(loginFormUrl)
         .method(Connection.Method.GET)
         .execute();

    Map<String, String> loginCookies = loginForm.cookies();

    Document document = Jsoup
        .connect(loginFormUrl)
        .data("akcio", akcio)
        .data("felnev",felnev)
        .data("jelszo",jelszo)
        .cookies(loginCookies)
        .post();

    Document document2 = Jsoup.connect(loggedInUrl)
    .cookies(loginCookies)
    .get();
    System.out.println(document2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM