【发布时间】:2015-10-03 10:36:26
【问题描述】:
我正在使用 Apache HttpClient 向我们的内部 API 服务器发送请求。服务器需要身份验证,并且需要使用身份验证令牌设置 cookie。
直到 HttpClient 4.3.6 这一直工作正常,但在 4.4 及更高版本上它已停止发送请求的 cookie。我的 cookie 域设置为 .subdomain.mycompany.com,适用于 4.3.6,但不适用于 4.4 及更高版本。如果我更具体并将完整的主机作为 cookie 域,即 host.subdomain.mycompany.com 它可以工作,但这不是解决方案。
这是一个类似于我正在做的代码sn-p:
public CloseableHttpResponse execute(CloseableHttpClient httpClient) throws IOException {
BasicClientCookie cookie = new BasicClientCookie("cookieName", "myAuthtoken");
cookie.setPath("/");
cookie.setDomain(".subdomain.mycompany.com");
cookie.setSecure(false);
HttpContext localContext = new BasicHttpContext(parentContext);
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
return httpClient.execute(target, request, localContext);
}
httpClient 已经构建并传递到设置身份验证 cookie 的代码中。
我看到了这个,类似于 Cookies getting ignored in Apache httpclient 4.4,但在我的情况下,cookie 没有被发送到服务器。
在 HttpClient 中打开线路日志记录后,我可以在 4.3.6 中看到以下内容,但在 4.4 及更高版本中看不到:
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: cookieName][value: authToken][domain: .subdomain.mycompany.com][path: /][expiry: Wed Jul 15 16:07:05 IST 2015] match [host.subdomain.mycompany.com:80/myApi]
这让我认为这与 cookie 域匹配有关。谁有想法?谢谢。
【问题讨论】:
-
这似乎是一个严重的错误。如果您尝试不带前导“.”的
cookie.setDomain("subdomain.mycompany.com");会怎样
标签: java cookies apache-httpclient-4.x