用于HTTPS抓取的Jsoup Cookies

Bri*_*ian 15 java cookies web-scraping jsoup

我正在尝试使用此网站在欢迎页面上收集我的用户名以学习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();
Run Code Online (Sandbox Code Playgroud)

我的cookie(.ASPXAUTH)总是以NULL结尾.如果我在webbrowser中删除此cookie,我将失去连接.所以我相信这是正确的cookie.另外,如果我改变代码

.cookie(".ASPXAUTH", "jkaldfjjfasldjf")  Using the correct values of course
Run Code Online (Sandbox Code Playgroud)

我可以从这个页面抓取我的登录名.这也让我觉得我有正确的cookie.那么,为什么我的饼干出现了?我的用户名和密码名称字段是否不正确?别的什么?

谢谢.

Igo*_*tos 36

我知道我迟到了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();
Run Code Online (Sandbox Code Playgroud)

虽然我仍然无法连接到某些网站,但我使用相同的基本代码连接到很多网站.哦,在我忘记之前..我认为我的问题是,SSL证书.你必须以一种我还没有想到的方式妥善管理它们.


Mar*_*szS 13

我总是分两步完成这个步骤(像普通人一样),

  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();
Run Code Online (Sandbox Code Playgroud)

并始终将cookie从previuos请求设置为下一个使用

         .cookies(response.cookies())
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 1

如果您尝试获取并传递所有 cookie,而不做如下假设,会怎么样:使用用户名和密码发送 POST 请求并保存会话 cookie

如果您仍然遇到问题,请尝试查看以下内容:Issues with moving cookie to GET request (after POST)