HttpClient 警告:Cookie 被拒绝:非法域属性

发布于 2024-12-05 01:56:48 字数 1537 浏览 4 评论 0原文

我正在使用 HttpClient 最新版本 (4.x)。现在我正在尝试执行 GET 请求。 我刚刚发布了一个获取请求。

这是我的守则;

public class Poster {

    static boolean routing1 = true, routing2 = true;
    static int counter1 = 0, counter2 = 0;
    DefaultHttpClient oHtp = null;
    HttpGet oHGet = null;
    HttpResponse oHRes = null;


    private void test(String fullAddress) throws Exception {
        oHtp = new DefaultHttpClient();
        oHGet = new HttpGet(fullAddress);

        HttpResponse response = oHtp.execute(oHGet);
        System.out.print(response.getStatusLine());

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            entity = new BufferedHttpEntity(entity);
            //  System.out.println(EntityUtils.toString(entity));
            System.out.print("\t entity is retrieved... ");
        }


        oHtp.getConnectionManager().shutdown();
    }
}

我只是很好地执行它。 第一个是

new Poster().test("http://123.xl.co.id/profile.php");

,第二个是

 new Poster().test("http://goklik.co.id/");

你,只有第二个......我收到了这个错误消息;

2011 年 9 月 18 日上午 10:11:30 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告:Cookie 被拒绝:“[版本:0][名称:CookiePst][值: 0149=xwGHF7HYDHLHQ84Isp/eSy9vu+Xq6cT12wxg1A==][域名: .mcore.com][路径:/][到期日:Sun Sep 18 10:38:59 ICT 2011]”。非法 域属性“mcore.com”。来源域名:“goklik.co.id”

我发现这里涉及到Cookie。但我不明白警告是什么意思。而且我也不知道如何解决它(Cookie 没有被拒绝)。希望有一点光明可以让我的思绪从你们身上消失......:D

I'm using HttpClient latest version (4.x). And right now I'm trying to do A GET Request.
I just posting a Get request.

This is my Code;

public class Poster {

    static boolean routing1 = true, routing2 = true;
    static int counter1 = 0, counter2 = 0;
    DefaultHttpClient oHtp = null;
    HttpGet oHGet = null;
    HttpResponse oHRes = null;


    private void test(String fullAddress) throws Exception {
        oHtp = new DefaultHttpClient();
        oHGet = new HttpGet(fullAddress);

        HttpResponse response = oHtp.execute(oHGet);
        System.out.print(response.getStatusLine());

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            entity = new BufferedHttpEntity(entity);
            //  System.out.println(EntityUtils.toString(entity));
            System.out.print("\t entity is retrieved... ");
        }


        oHtp.getConnectionManager().shutdown();
    }
}

I just execute it nicely.
First is

new Poster().test("http://123.xl.co.id/profile.php");

and second is

 new Poster().test("http://goklik.co.id/");

ya, And Only the Second one.... I got this The error message;

Sep 18, 2011 10:11:30 AM
org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Cookie rejected: "[version: 0][name: CookiePst][value:
0149=xwGHF7HYDHLHQ84Isp/eSy9vu+Xq6cT12wxg1A==][domain:
.mcore.com][path: /][expiry: Sun Sep 18 10:38:59 ICT 2011]". Illegal
domain attribute "mcore.com". Domain of origin: "goklik.co.id"

I realized that the Cookie is involved here. But I don't understand what the Warning means. And I also don't know how to solve it (Cookie not being rejected). Hope there is a bit of light to clear my mind from you guys.... :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

云胡 2024-12-12 01:56:48

也许为时已晚,但我遇到了同样的问题,并且找到了一些可以帮助我解决问题的方法,只需设置 Cookie 策略 与浏览器兼容性:

httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
        CookiePolicy.BROWSER_COMPATIBILITY);

以下是可能的值:

cookie策略提供了相应的cookie管理接口
对于给定类型或版本的 cookie。

默认使用 RFC 2109 规范。其他支持
可以在适当时选择规格或在时设置默认值
想要的

提供以下规格:

  • BROWSER_COMPATIBILITY:与常见的 Cookie 管理实践兼容(即使它们并非 100% 标准兼容)
  • NETSCAPE:符合 Netscape cookie 草案
  • RFC_2109:符合 RFC2109(默认)
  • IGNORE_COOKIES:不自动处理 cookie

Maybe it's too late, but I had the same problem and I've found something that helped me work it out, just set the Cookie Policy to Browser Compability:

httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
        CookiePolicy.BROWSER_COMPATIBILITY);

Here are the possible values:

The cookie policy provides corresponding cookie management interfrace
for a given type or version of cookie.

RFC 2109 specification is used per default. Other supported
specification can be chosen when appropriate or set default when
desired

The following specifications are provided:

  • BROWSER_COMPATIBILITY: compatible with the common cookie management practices (even if they are not 100% standards compliant)
  • NETSCAPE: Netscape cookie draft compliant
  • RFC_2109: RFC2109 compliant (default)
  • IGNORE_COOKIES: do not automcatically process cookies
无风消散 2024-12-12 01:56:48

你无法“修复”它。该网站正在尝试设置一个不允许设置的 cookie,并且您正在使用的 apache 客户端库正在告诉您这一点。

当域名为 goklik.co.id 时,它尝试为 mcore.com 设置 cookie

You can't "fix" it. The site is trying to set a cookie it's not allowed to set and the apache client library you're using is telling you about it.

It's trying to set a cookie for mcore.com when the domain is goklik.co.id

夏日浅笑〃 2024-12-12 01:56:48

httpclient 4.3 之前,同一页面中的这个答案很酷。

但是 httpclient 4.3以来,API似乎发生了很大变化,以下代码可以工作:

RequestConfig customizedRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
HttpClientBuilder customizedClientBuilder = HttpClients.custom().setDefaultRequestConfig(customizedRequestConfig);
CloseableHttpClient client = customizedClientBuilder.build(); // customized client,

Before httpclient 4.3, this answer in the same page is cool.

But since httpclient 4.3, API seems changed a lot, following code would work:

RequestConfig customizedRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
HttpClientBuilder customizedClientBuilder = HttpClients.custom().setDefaultRequestConfig(customizedRequestConfig);
CloseableHttpClient client = customizedClientBuilder.build(); // customized client,
-黛色若梦 2024-12-12 01:56:48

我正在使用 http 客户端 4.5.2,这是设置 cookie 规范来轻松解决我的问题。如何实例化客户端的示例:

httpClient = HttpClients.custom()
                .setDefaultRequestConfig(RequestConfig.custom()
                        // Waiting for a connection from connection manager
                        .setConnectionRequestTimeout(10000)
                        // Waiting for connection to establish
                        .setConnectTimeout(5000)
                        .setExpectContinueEnabled(false)
                        // Waiting for data
                        .setSocketTimeout(5000)
                        .setCookieSpec("easy")
                        .build())
                .setMaxConnPerRoute(20)
                .setMaxConnTotal(100)
                .build();

I am using http client 4.5.2 and this is set cookie spec to easy solved my problem. The example of how instantiate client:

httpClient = HttpClients.custom()
                .setDefaultRequestConfig(RequestConfig.custom()
                        // Waiting for a connection from connection manager
                        .setConnectionRequestTimeout(10000)
                        // Waiting for connection to establish
                        .setConnectTimeout(5000)
                        .setExpectContinueEnabled(false)
                        // Waiting for data
                        .setSocketTimeout(5000)
                        .setCookieSpec("easy")
                        .build())
                .setMaxConnPerRoute(20)
                .setMaxConnTotal(100)
                .build();
月朦胧 2024-12-12 01:56:48

以下是在 v4.5.x4.5.6 now)中抑制警告的最简单方法:

HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.disableCookieManagement();
HttpClient httpClient = clientBuilder.build();

Here is the simplest way to suppress the warning in v4.5.x (4.5.6 right now) :

HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.disableCookieManagement();
HttpClient httpClient = clientBuilder.build();
叫思念不要吵 2024-12-12 01:56:48

只是想改进埃里克的答案,因为它不能直接解决我的情况,但将 CookieSpecs 更改为 IGNORE_COOKIES 可以解决我的问题。

RequestConfig customizedRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
HttpClientBuilder customizedClientBuilder = 
HttpClients.custom().setDefaultRequestConfig(customizedRequestConfig);
CloseableHttpClient client = customizedClientBuilder.build(); // customized client,

因为在我的 HttpClient 4.5 版本中 CookieSpecs.BROWSER_COMPATIBILITY 已经被折旧。

Just want to improve Eric's answer, as it doesnt directly solve my scenario but changing CookieSpecs to IGNORE_COOKIES solves my problem.

RequestConfig customizedRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
HttpClientBuilder customizedClientBuilder = 
HttpClients.custom().setDefaultRequestConfig(customizedRequestConfig);
CloseableHttpClient client = customizedClientBuilder.build(); // customized client,

Because in my version of HttpClient 4.5 CookieSpecs.BROWSER_COMPATIBILITY is already depreciated.

翻身的咸鱼 2024-12-12 01:56:48

如果您不需要处理 cookie,您可以使用 org.apache.http.impl.cookie.IgnoreSpecProvider 或 org.apache.http.impl.cookie.IgnoreSpec 禁用它。 /code> 取决于您使用的 API。在 HttpClientBuilder 上调用 disableCookieManagement() 是不够的

If you don't need to process cookies you can simply disable it, with org.apache.http.impl.cookie.IgnoreSpecProvider or org.apache.http.impl.cookie.IgnoreSpec depending on what API you use. Calling disableCookieManagement() on HttpClientBuilder is not enough

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文