简体   繁体   English

如何使用Jsoup登录我的大学网站?

[英]How to use Jsoup to login my university website?

I am trying to come up with a Android app that needs some information on the university inner website. 我正在尝试提出一个需要在大学内部网站上提供一些信息的Android应用程序。 I have been trying to use Jsoup to login the website programmatically. 我一直在尝试使用Jsoup以编程方式登录网站。 Here is the code I have now: 这是我现在拥有的代码:

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
//import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
//import org.jsoup.select.Elements;


import java.io.IOException;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        Document doc;
        try {
            Connection.Response res = Jsoup
                    .connect(
                            "https://sso.bris.ac.uk/sso/login?service=https%3A%2F%2Fwww.cs.bris.ac.uk%2FTeaching%2Fsecure%2Funit-list.jsp%3Flist%3Dmine")
                    .execute();
            Map<String, String> cookies = res.cookies();
            System.out.println(cookies.keySet());
            Document fakepage = res.parse();
            Element fakelt = fakepage.select("input[name=lt]").get(0);
            Element fakeexecution = fakepage.select("input[name=execution]")
                    .get(0);
            Element fake_eventID = fakepage.select("input[name=_eventId]").get(
                    0);
            System.out.println("Hello World!");
            System.out.println(fakelt.attr("value"));
            System.out.println(fakeexecution.toString());
            System.out.println(fake_eventID.toString());
//          System.out.println(cookies.get("JSESSIONID"));
            String url="https://sso.bris.ac.uk/sso/login?service=https%3A%2F%2Fwww.cs.bris.ac.uk%2FTeaching%2Fsecure%2Funit-list.jsp%3Flist%3Dmine";
            System.out.println(url);
            Connection newreq = Jsoup
                    .connect(url)
                    .cookies(cookies).data("lt", fakelt.attr("value")).followRedirects(true).header("Connection", "keep-alive")
                    .header("Refer", " https://sso.bris.ac.uk/sso/login?service=https%3A%2F%2Fwww.cs.bris.ac.uk%2FTeaching%2Fsecure%2Funit-list.jsp%3Flist%3Dmine")
                    .header("Content-Type","application/x-www-form-urlencoded;charset=UTF-8")
                    .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0")
                    .data("lt",fakelt.attr("value"))
                    .data("execution", fakeexecution.attr("value"))
                    .data("_eventID", fake_eventID.attr("value"))
                    .data("username", "aabbcc").data("password", "ddeeff")
                    .data("submit", "").method(Method.POST);
            Connection.Response newres = newreq.execute();
            doc = newres.parse();
            System.out.println(doc.toString());
            System.out.println(newres.statusCode());


        Map<String,String> newcookies = newres.cookies();
        doc = Jsoup.connect("https://www.cs.bris.ac.uk/Teaching/secure/unit-list.jsp?list=mine").cookies(newcookies).get();
        System.out.println(doc.toString());
//          System.out.println(doc.toString());
        } catch (IOException e) {
            System.out.println("Excepiton:");
            System.out.println(e.getMessage());
        }
    }
}

I completely faked a form to submit use Jsoup, and to get around the security cookies I first request the website once and then use the cookies it sent me to request the website again. 我完全伪造了一个表单,以提交使用Jsoup的信息,并且为了避开安全性cookie,我首先请求该网站一次,然后使用发送给我的cookie再次请求该网站。 The form has some hidden fields so I use the ones I got on my first request to fake it when I request it again. 该表单有一些隐藏的字段,因此当我再次请求它时,我会使用在第一次请求时得到的那些字段来伪造它。 However this does not work. 但是,这不起作用。 Is it possible to do it or the server has some advanced preventer against me doing so? 是否可以这样做,或者服务器是否有针对我的高级防护措施?

Do not use Jsoup to do this, it needs you to handle all the cookies yourself, instead, use Httpclient, if you use something from 4.0 onward it handle the cookies automatically. 不要使用Jsoup来执行此操作,它需要您自己处理所有cookie,相反,请使用Httpclient,如果您使用4.0以后的版本,它将自动处理cookie。 Much eaiser to work with. 很容易合作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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