jsoup to login to a webite

二次信任 提交于 2020-01-16 01:19:12

问题


I am trying to use jsoup to get information after logging into "http://pawscas.usask.ca/cas-web/login". I've tried what's below and it doesn't seem to work, any help would be appreciated, thanks.

Connection.Response res = null;
    try {
        res = Jsoup.connect("http://pawscas.usask.ca/cas-web/login")
            .data("username", "user") 
            .data("password", "pass")
            //.data("It", "some data")
            //.data("execution", "some data")
            //.data("_eventId", "submit")
            .method(Method.POST)
            .execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

     //System.out.println(res.cookies());

    //This will get you cookies
    Map<String, String> loginCookies = res.cookies();

    Document doc = null;
    try {
        doc = Jsoup.connect("https://paws5.usask.ca/web/home-community#mycourses")
          .cookies(loginCookies)
          .get();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(doc.toString());

回答1:


Try this buddy, basically the problem seems to be that the data you provided along with your request was not sufficient enough, you need to include ALL input fields.

And two of the inputs are a randomly generated number which seems to be associated with your session (the one with the name lt and execution), so first you need to get them, then pass it along with your data.

Connection.Response initialResponse = null;
    try {
        // get "lt" and "execution" value
        initialResponse = = Jsoup.connect("http://pawscas.usask.ca/cas-web/login").method(Method.GET).execute();
        Document doc = initialResponse.parse();

        // get lt
        Element lt = doc.select("input[name=lt]").first();
        String ltVal = lt.attr("value");

        // get execution
        Element execution = doc.select("input[name=execution]").first();
        String executionVal = execution.attr("value");

        // get cookies
        Map<String, String> cookies = initialResponse.cookies();

        // now do the login
        res = Jsoup.connect("http://pawscas.usask.ca/cas-web/login")
            .data("username", "user") 
            .data("password", "pass")
            .data("lt", ltVal)
            .data("execution", executionVal)
            .data("_eventId", "submit")
            .cookies(cookies)
            .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36")
            .method(Method.POST)
            .execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

     //System.out.println(res.cookies());

    cookies.putAll(res.cookies());

    Document doc = null;
    try {
        doc = Jsoup.connect("https://paws5.usask.ca/web/home-community#mycourses")
          .cookies(cookies)
          .get();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(doc.toString());



回答2:


There are hidden parameters in that form that the server may be checking. Without knowing more about your error code, or what the server is doing, it's hard to answer your question. You can try doing a get first, with jsoup, find the values of the hidden parameters and post them back to the server to see if that helps, but if it doesn't then I don't know what:

<input type="hidden" name="lt" value="LT-3939609-Kw1VcYGxuNLKyEukchGOiWiBCzx0Mh" />
<input type="hidden" name="execution" value="e1s1" />
<input type="hidden" name="_eventId" value="submit" />


来源:https://stackoverflow.com/questions/34584302/jsoup-to-login-to-a-webite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!