当前位置: 动力学知识库 > 问答 > 编程问答 >

java - 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);
分享给朋友:
您可能感兴趣的文章:
随机阅读: