问题描述
在Android应用程序中,当使用 c $>
$ b
首先,创建一个简单的 CookieSpec ,它将接受 null 和 expires 属性的空值,如下所示:
class LenientCookieSpec extends BrowserCompatSpec {
public LenientCookieSpec(){
super();
registerAttribHandler(ClientCookie.EXPIRES_ATTR,new BasicExpiresHandler(DATE_PATTERNS){
@Override public void parse(SetCookie cookie,String value)throws MalformedCookieException {
if(TextUtils.isEmpty(value)){
//你应该在cookie中设置你想要的任何内容
cookie.setExpiryDate(null);
} else {
super.parse(cookie,value);
}
}
});
}
}
现在您需要注册&在您的HTTP客户端中选择此新的 CookieSpec 。
DefaultHttpClient client = new DefaultHttpClient();
client.getCookieSpecs()。register(lenient,new CookieSpecFactory(){
public CookieSpec newInstance(HttpParams params){
return new LenientCookieSpec();
}
});
HttpClientParams.setCookiePolicy(client.getParams(),lenient);
这样的东西可以为你工作。
In an android application, when using DefaultHttpClient to get an URL content (executing HttpGet) I receive the following warning in logs:
W/ResponseProcessCookies(20386): Invalid cookie header: "Set-Cookie: NSC_vbue_iuuq=ffff660; expires=; domain=private.false.name; path=/; isSecure=false". Unable to parse expires attribute:
I understand the warning because the expires field does not contain a valid date format. I understand it maybe because it is a 'session cookie' (without being expert). Thread about similar situation in Curl context
Searching the web I found mainly the
.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH (or other) )
option to avoid warning by parsing correctly dates that contain a comma.
However, I would like to avoid that log. (not by disabling logs)I believe internally all is fine since "I GUESS", cookie.setExpiryDate() is simply not called.
Do you think I need a specific configuration of my HTTP client (I've not set specific configurations) to avoid that warning or to support empty expires?
Thanks.
If you do not mind altering the CookieSpec you can supply your own, more lenient, subclass.
First, create a lenient CookieSpec that will accept null and empty values for the expires attribute, like this:
class LenientCookieSpec extends BrowserCompatSpec { public LenientCookieSpec() { super(); registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(DATE_PATTERNS) { @Override public void parse(SetCookie cookie, String value) throws MalformedCookieException { if (TextUtils.isEmpty(value)) { // You should set whatever you want in cookie cookie.setExpiryDate(null); } else { super.parse(cookie, value); } } }); } }
Now you need to register & choose this new CookieSpec in your HTTP client.
DefaultHttpClient client = new DefaultHttpClient(); client.getCookieSpecs().register("lenient", new CookieSpecFactory() { public CookieSpec newInstance(HttpParams params) { return new LenientCookieSpec(); } }); HttpClientParams.setCookiePolicy(client.getParams(), "lenient");
Something "like this" could work for you.
这篇关于Cookie标头无效:当expires属性为空时,无法解析expires属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!