Invalid cookie header : Unable to parse expires attribute when expires attribute is empty

后端 未结 2 1686
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 21:40

In an android application, when using DefaultHttpClient to get an URL content (executing HttpGet) I receive the following warning in logs:

W/Re         


        
2条回答
  •  别那么骄傲
    2020-12-16 22:38

    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.

提交回复
热议问题