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

标签 java http cookies jsoup

我正在尝试开发一个 Android 应用程序,该应用程序需要大学内部网站上的一些信息。我一直在尝试使用 Jsoup 以编程方式登录网站。这是我现在的代码:

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

我完全伪造了一个表单来提交使用 Jsoup,为了绕过安全 cookie,我首先请求该网站一次,然后使用它发送给我的 cookie 再次请求该网站。该表单有一些隐藏字段,所以我使用第一次请求时得到的字段在我再次请求时伪造它。但是,这不起作用。是否可以这样做,或者服务器有一些高级阻止程序阻止我这样做?

最佳答案

不要使用 Jsoup 来做这个,它需要你自己处理所有的 cookies,相反,使用 Httpclient,如果你使用 4.0 以后的东西它会自动处理 cookies。使用起来更容易。

关于java - 如何使用 Jsoup 登录我的大学网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22078592/

相关文章:

node.js - 对 HEAD 请求使用 express sendFile

javascript - 从父级访问在子域上设置的 cookie

javascript - 在浏览器中加密 Cookie

java - 泛型类的数组

java - 有没有办法访问之前创建的 Guice 注入(inject)器?

php - 我应该以哪种格式期望提交的文本区域中的换行符

api - GAE API 资源管理器不显示 API,似乎卡在加载中

java - 具有对特定子类的继承限制的 JPA OneToMany 集合

java - 为什么 Java Thread.sleep 或 Date.after() 在其他计算机上的功能不同?

c# - 将 session cookie 设置为 HttpOnly