java 获取“Set-Cookie”标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13302245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "Set-Cookie" header
提问by pomkine
I'm trying to get 'Set Cookie' header using apache httpclietn-4.2.2 and having some problems.
我正在尝试使用 apache httpclietn-4.2.2 获取“设置 Cookie”标头,但遇到了一些问题。
Header in Firebug:
Firebug 中的标题:
Set-Cookie remixreg_sid=deleted; expires=Thu, 10-Nov-2011 04:32:30 GMT; path=/;
domain=.vk.com remixapi_sid=deleted; expires=Thu, 10-Nov-2011 04:32:30 GMT; path=/;
domain=.vk.com remixrec_sid=deleted; expires=Thu, 10-Nov-2011 04:32:30 GMT; path=/;
domain=.vk.com remixsid=0000000000000000000000000000000000000000000000000000; expires=Mon, 04-Nov-2013 16:10:24 GMT; path=/; domain=.vk.com
How I'm trying to obtain it:
我如何尝试获取它:
//location is a header with url I need to do GET request to
Header location = response.getFirstHeader("Location");
HttpGet httpGet = new HttpGet(location.getValue());
httpClient.getParams().setParameter(
//tried to use different policies
ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);
Header [] allHeaders=response.getAllHeaders();
In allHeaders I have all headers except "Set Cookie". And I have warnings like this:
在 allHeaders 中,我拥有除“Set Cookie”之外的所有标题。我有这样的警告:
WARNING: Invalid cookie header: "Set-Cookie: remixlang=0; expires=Mon, 18-Nov-2013
03:21:47 GMT; path=/; domain=.vk.com". Unrecognized cookie header 'Set-Cookie:
remixlang=0; expires=Mon, 18-Nov-2013 03:21:47 GMT; path=/; domain=.vk.com'
Nov 09, 2012 4:31:41 AM org.apache.http.client.protocol.ResponseProcessCookies
processCookies
So I think the problem is with 'expires' date.
所以我认为问题在于“到期”日期。
What I tried to do:
我试图做的事情:
1) Invalid cookie header : Unable to parse expires attribute when expires attribute is emptyCreated custom CookieSpec and tried to use it:
1) cookie 标头无效:当过期属性为空时无法解析过期属性创建自定义 CookieSpec 并尝试使用它:
httpClient.getCookieSpecs().register("vkCookie", new CookieSpecFactory() {
public CookieSpec newInstance(HttpParams params){
return new VkCookieSpec();
}
});
HttpClientParams.setCookiePolicy(httpClient.getParams(), "vkCookie");
2) Tried to set Data Format in httpClient params :
2) 尝试在 httpClient 参数中设置数据格式:
httpClient.getParams().setParameter(CookieSpecPNames.DATE_PATTERNS, Arrays.asList("EEE, dd-MMM-yyyy HH:mm:ss z"));
But I'm still getting that warning. Would appreciate any help.
但我仍然收到那个警告。将不胜感激任何帮助。
采纳答案by ok2c
You are trying to parse 'Set-Cookie' header with the RFC 2965 compliant spec, whereas RFC 2965 accepts 'Set-Cookie2' headers only.
The cookie in question is malformed. It contains non-standard 'expires' attribute, which, to make matters worse, contains a reserved character (comma) without enclosing quote marks. However, given it is a very common protocol violation HttpClient should be able to parse this cookie using 'best_match', 'browser_compatibility' or 'netscape_draft' policies.
您正在尝试使用符合 RFC 2965 的规范解析“Set-Cookie”标头,而 RFC 2965 仅接受“Set-Cookie2”标头。
有问题的 cookie 格式不正确。它包含非标准的 'expires' 属性,更糟糕的是,它包含一个不带引号的保留字符(逗号)。然而,鉴于这是一个非常常见的协议违规,HttpClient 应该能够使用“best_match”、“browser_compatibility”或“netscape_draft”策略解析这个 cookie。
In fact, one should always be using the 'best_match' policy and let HttpClient pick up the best matching policy based on the composition of the cookie headers.
事实上,应该始终使用“best_match”策略,让 HttpClient 根据 cookie 标头的组成选择最佳匹配策略。
回答by muelleth
I know this is an old question. But I had the same problem and just wanted to post my snippet to solve it, in particular setting CookieSpecs.STANDARD
explicitly (see spec on apache commonsfor details):
我知道这是一个老问题。但是我遇到了同样的问题,只想发布我的代码片段来解决它,特别是CookieSpecs.STANDARD
明确设置(有关详细信息,请参阅apache commons 规范):
RequestConfig globalConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(globalConfig)
.build();
RequestConfig localConfig = RequestConfig.copy(globalConfig)
.setCookieSpec(CookieSpecs.STANDARD)
.build();
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(localConfig);
// Request
CloseableHttpResponse response = httpClient.execute(httpGet);
Hope this helps.
希望这可以帮助。