用于 HTTPS 抓取的 Jsoup Cookies

2022-09-01 18:09:28

我正在尝试这个网站,在欢迎页面上收集我的用户名,以学习Jsoup和Android。使用以下代码

Connection.Response res = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx")
    .data("ctl00$ContentPlaceHolder1$ctl00$Login1$UserName", "username", "ctl00$ContentPlaceHolder1$ctl00$Login1$Password", "password")
    .method(Method.POST)
    .execute();
String sessionId = res.cookie(".ASPXAUTH");

Document doc2 = Jsoup.connect("http://www.mikeportnoy.com/forum/default.aspx")
.cookie(".ASPXAUTH", sessionId)
.get();

我的饼干 (.ASPXAUTH) 始终以 NULL 结尾。如果我在网络浏览器中删除此 Cookie,则会失去连接。所以我确信这是正确的饼干。另外,如果我更改代码

.cookie(".ASPXAUTH", "jkaldfjjfasldjf")  Using the correct values of course

我可以从此页面抓取我的登录名。这也让我觉得我有正确的饼干。那么,为什么我的饼干会变成空的呢?我的用户名和密码名称字段是否不正确?别的?

谢谢。


答案 1

我知道我在这里迟到了10个月。但是使用Jsoup的一个好选择是使用这个简单的代码段:

//This will get you the response.
Response res = Jsoup
    .connect("url")
    .data("loginField", "login@login.com", "passField", "pass1234")
    .method(Method.POST)
    .execute();

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

//And this is the easieste way I've found to remain in session
Documente doc = Jsoup.connect("url").cookies(cookies).get();

虽然我仍然无法连接到某些网站,但我使用相同的基本代码连接到其中的许多网站。哦,在我忘记之前。我认为我的问题是SSL证书。你必须以一种我仍然没有完全弄清楚的方式正确管理它们。


答案 2

我总是分两步完成(像正常人一样),

  1. 读取登录页面(通过 GET,读取 Cookie)
  2. 提交表格和 Cookie(通过 POST,无需操作 Cookie)

例:

Connection.Response response = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx")
        .method(Connection.Method.GET)
        .execute();

response = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx")
        .data("ctl00$ContentPlaceHolder1$ctl00$Login1$UserName", "username")
        .data("ctl00$ContentPlaceHolder1$ctl00$Login1$Password", "password")
        .cookies(response.cookies())
        .method(Connection.Method.POST)
        .execute();

Document homePage = Jsoup.connect("http://www.mikeportnoy.com/forum/default.aspx")
        .cookies(response.cookies())
        .get();

并始终将 Cookie 从 previuos 请求设置为 next 使用

         .cookies(response.cookies())

SSL在这里并不重要。如果证书有问题,请执行此方法以忽略 SSL。

public static void trustEveryone() {
    try {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[]{new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        }}, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    } catch (Exception e) { // should never happen
        e.printStackTrace();
    }
}

推荐