【问题标题】:Java Apache HttpClient some Cookies are missingJava Apache HttpClient 缺少一些 Cookie
【发布时间】:2013-08-30 13:23:47
【问题描述】:

我搜索了很长时间,但没有找到任何对我的问题真正有用的东西。 I used this as a guide,向下滚动到:“3. Apache HttpClient – 自动登录 Google”。

当您使用 Chrome 时,您可以查找用于当前页面的 Cookie。 登录并浏览网站的其他页面后,这些Cookies比HttpClient登录并浏览其他页面后获得的要多得多。 HttpClient 仅获得 Chrome 中显示的十一个中的两个。 页面需要这些 Cookie,因为我收到的 HTML 仅包含您未登录时获得的预览。
一些缺少的 Cookie 称为“userauth_name”、“member_id”等。我认为它们是登录所必需的,对吧? :P。

我从指南代码中更改的唯一内容是:
- 当然是登录的 URL 和要加载的其他页面
- 在 sendPost:
主机:post.setHeader("Host", "accounts.google.com");
推荐人:post.setHeader("Referer", "https://accounts.google.com/ServiceLoginAuth");
- 在 getFormParams
在我的测试页面上,表单标签没有 id 属性,所以我做了这个:

    Elements forms = doc.getElementsByTag("form");
    Element loginform = null;
    for (Element inputElement : forms) {
        if (inputElement.attr("name").equals("authform"))
            loginform = inputElement;
    }
    Elements inputElements = loginform.getElementsByTag("input");

    List<NameValuePair> paramList = new ArrayList<NameValuePair>();

    for (Element inputElement : inputElements) {
        String key = inputElement.attr("name");
        String value = inputElement.attr("value");

        if (key.equals("username"))
            value = username;
        else if (key.equals("password"))
            value = password;

        paramList.add(new BasicNameValuePair(key, value));          

    }

在指南中的标题之后是代码中的基本步骤:
1. 发送 GET 请求以获取登录表单。 (我得到响应码 = 200)
2.使用jsoup html解析器抓取表单输入。
3.构造参数并发出POST请求进行认证。 (响应码 = 200)
4. 向 Gmail 发送另一个 GET 请求。 (响应码 = 200)

没有服务器错误或 java 错误。

又是一个问题:为什么缺少一些 Cookie?

【问题讨论】:

  • 我解决了我的问题。我刚刚将所有需要的 Cookie 手动添加到 Cookiestore 作为“新的 BasicClientCookie”。除了手动添加之外,我仍然希望看到其他解决方案。

标签: java apache cookies session-cookies apache-httpclient-4.x


【解决方案1】:

某些 cookie 可能会因违反实际 cookie 管理政策而被拒绝。通常情况下是否是这种情况很容易通过打开上下文日志来确定,如this guide 中所述。

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多