无效的 cookie 标头:当过期属性为空时无法解析过期属性 [英] Invalid cookie header : Unable to parse expires attribute when expires attribute is empty

查看:29
本文介绍了无效的 cookie 标头:当过期属性为空时无法解析过期属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 android 应用程序中,当使用 DefaultHttpClient 获取 URL 内容(执行 HttpGet)时,我在日志中收到以下警告:

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:

我理解警告,因为 expires 字段不包含有效的日期格式.我理解它可能是因为它是一个会话 cookie"(不是专家).关于 Curl 上下文中类似情况的主题

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.

但是,我想避免该日志.(不是通过禁用日志)我相信内部一切都很好,因为我猜",cookie.setExpiryDate() 根本没有被调用.

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.

你认为我需要对我的 HTTP 客户端进行特定配置(我没有设置特定配置)来避免该警告或支持空过期?

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?

谢谢.

推荐答案

如果您不介意更改 CookieSpec,您可以提供您自己的更宽松的子类.

If you do not mind altering the CookieSpec you can supply your own, more lenient, subclass.

首先,创建一个宽松的 CookieSpec,它将接受 nullexpires 属性的空值,如下所示:

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);
                }
            }
        });
    }
}

现在您需要注册 &在您的 HTTP 客户端中选择这个新的 CookieSpec.

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 标头:当过期属性为空时无法解析过期属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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