使用jsoup登录此页失败

vfwfrxfs  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(307)

我一直在努力登录到这个页面,这样我就可以刮私人
learn.sun.ac.za/我的
现在呼叫一会儿。我已经搜索了多个so帖子,并试图应用每个帖子的建议,但都没有效果。
尝试1

String url = "https://sso-prod.sun.ac.za/cas/login?service=http%3A%2F%2Flearn.sun.ac.za%2Flogin%2Findex.php";
String userAgent = "Mozilla/5.0";

            Connection.Response response = Jsoup.connect(url)
                    .userAgent(userAgent)
                    .method(Connection.Method.GET)
                    .execute();

            response = Jsoup.connect(url)
                    .userAgent(userAgent)
                    .cookies(response.cookies())
                    .data("action", "login")
                    .data("username", "MYUSERNAME")
                    .data("password", "MYPASSWORD")
                    .method(Connection.Method.POST)
                    //.followRedirects(true)
                    .execute();            

            Document doc = Jsoup.connect("http://learn.sun.ac.za/my")
                    .cookies(response.cookies())
                    .userAgent(userAgent)
                    .get();

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

输出:“单一登录|公司”
表示它没有登录。
根据其他帖子中的建议,我通过chrome监控了流量,并将所有标题添加到代码中

String url = "https://sso-prod.sun.ac.za/cas/login?service=http%3A%2F%2Flearn.sun.ac.za%2Flogin%2Findex.php";
String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36";

            Connection.Response response = Jsoup.connect(url)
                    .userAgent(userAgent)
                    .method(Connection.Method.GET)
                    .execute();

            response = Jsoup.connect(url)
                    .userAgent(userAgent)
                    .cookies(response.cookies())
                    .header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                    .header("accept-encoding", "gzip, deflate")
                    .header("accept-language", "en-US,en;q=0.8")
                    .header("cache-control", "max-age=0")
                    .header("connection", "keep-alive")
                    .header("content-length", "114")
                    .header("content-type","application/x-www-form-urlencoded")
                    .header("dnt", "1")
                    .header("host", "sso-prod.sun.ac.za")
                    .header("origin", "https://sso-prod.sun.ac.za" )
                    .header("referer", "https://sso-prod.sun.ac.za/cas/login?service=http%3A%2F%2Flearn.sun.ac.za%2Flogin%2Findex.php")
                    .header("upgrade-insecure-requests", "1")
                    .data("action", "login")
                    .data("username", "MYUSERNAME")
                    .data("password", "MYPASSWORD")
                    .data("lt", "LT-3042474-9t3oldTU1253G6HVqFffHgMWxnYXdg")
                    .data("execution", "e1s1" )
                    .data("_eventId", "submit")
                    .method(Connection.Method.POST)
                    //.followRedirects(true)
                    .execute();            

            Document doc = Jsoup.connect("http://learn.sun.ac.za/my")
                    .cookies(response.cookies())
                    .userAgent(userAgent)
                    .get();

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

结果是一样的。之后我做的是打印出实际的html代码,发现代码中没有任何登录错误消息,这意味着我在某个地方搞砸了,实际上还没有提交表单?
这就是一个成功的chrome登录的样子

vjrehmav

vjrehmav1#

那是因为你的 LT 值是错误的-您应该获得每个新会话的值,而不是像您那样发送一些值。看看这个 HTML 页面的位置:

的选择器 DIV 包含值的 div.controls:nth-child(1) .
所以步骤是-
加载页面
获取的值 LT 将其添加到您的 POST 请求。

相关问题