Cookie在Apache httpclient 4.4中被忽略 [英] Cookies getting ignored in Apache httpclient 4.4

查看:438
本文介绍了Cookie在Apache httpclient 4.4中被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Apache http客户端从4.3.6升级到4.4,并发现cookie被忽略了.知道如何让Cookie在4.4中工作吗?

I upgraded Apache http client from 4.3.6 to 4.4 and observed that cookies are getting ignored. Any idea how to get cookie working in 4.4?

代码段

CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(new BasicClientCookie("name", "value"));
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();
HttpClient client = HttpClientBuilder.create()
    .disableRedirectHandling()
    .setDefaultRequestConfig(config)
    .setDefaultCookieStore(cookieStore)
    .build();

我尝试了CookieSpecs.DEFAULTCookieSpecs.STANDARDCookieSpecs.STANDARD_STRICT,但似乎都无法正常工作.

I tried CookieSpecs.DEFAULT, CookieSpecs.STANDARD and CookieSpecs.STANDARD_STRICT but none seem to work.

推荐答案

我已经用4.3.6和4.5版本执行了示例代码.在4.3.6中,我使用了RequestConfig.DEFAULT,并且运行良好.使用4.5,它将返回

I have executed the example code with both versions 4.3.6 and 4.5 . With 4.3.6 I used RequestConfig.DEFAULT and it was working fine. With 4.5 it returns

java.lang.NullPointerException: while trying to invoke the method java.lang.String.equalsIgnoreCase(java.lang.String) of a null object loaded from local variable 'domain'
at org.apache.http.impl.cookie.PublicSuffixDomainFilter.match(PublicSuffixDomainFilter.java:76)
at org.apache.http.impl.cookie.CookieSpecBase.match(CookieSpecBase.java:135)
at org.apache.http.impl.cookie.DefaultCookieSpec.match(DefaultCookieSpec.java:177)
at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:165)

更改已于14/19/14,10:59 PM添加到修订版1646864:

The change was added with revision 1646864 on 12/19/14, 10:59 PM:

符合RFC 6265的Cookie规范

RFC 6265 compliant cookie spec

为了使版本4.5能够正常运行,您需要将域设置为cookie,并且该域也不等于确切的主机 org.apache.http.cookie.ClientCookie.DOMAIN_ATTR 必须设置:

In order to make things work with version 4.5 you will need to set domain to the cookie and it the domain does not equal the exact host also org.apache.http.cookie.ClientCookie.DOMAIN_ATTR must be set:

    BasicClientCookie cookie = new BasicClientCookie("cookieName", "cookieValue");
    cookie.setDomain(".my.domain.com");
    cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
    CookieStore cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);

这篇关于Cookie在Apache httpclient 4.4中被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆