jsoup登录到网站 [英] jsoup to login to a webite

查看:195
本文介绍了jsoup登录到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jsoup登录" http://pawscas.usask之后获取信息.ca/cas-web/login ".我已经尝试了下面的内容,但似乎没有用,谢谢您的任何帮助.

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

推荐答案

尝试这个伙伴,基本上问题似乎是您提供的数据与请求不足够,您需要包括所有输入字段.

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.

其中两个输入是一个随机生成的数字,似乎与您的会话相关联(一个名为ltexecution的数字),因此首先需要获取它们,然后将其与您的会话一起传递数据.

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

这篇关于jsoup登录到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆