How do you specify a cookie policy in htmlunit to accept all cookies?
-
@Mat Banik, I post some answer, but it isn't really good. – Stan Kurilin Jan 14 '11 at 17:58
-
I have found that rather than tweaking the cookie policy, it is better to see why the cookies are rejected - and fix. – mauhiz Jul 15 '14 at 04:23
2 Answers
Just recreate the whole CookieManager class: Here is source of the class: http://jarvana.com/jarvana/view/net/sourceforge/htmlunit/htmlunit/2.8/htmlunit-2.8-sources.jar!/com/gargoylesoftware/htmlunit/CookieManager.java?format=ok
Now lookup this method public synchronized Set<Cookie> getCookies(final URL url)
in there you find this:
public static final String HTMLUNIT_COOKIE_POLICY = CookiePolicy.BROWSER_COMPATIBILITY; //default
final CookieSpec spec = registry_.getCookieSpec(HTMLUNIT_COOKIE_POLICY);
for (final org.apache.http.cookie.Cookie cookie : all) {
if (spec.match(cookie, cookieOrigin)) {
matches.add(cookie);
}
}
Remote the specs matching statement if (spec.match(cookie, cookieOrigin))
you you should accept all cookies regardless on policy. And/or you can work up ACCEPT_ALL_COOKIES flag and by pass the specs matching if that is the policy indicated in the configuration.

- 26,356
- 39
- 116
- 178
Some solutions with source code modifications.
You can simply remove
cookieSpec.validate(cookie, cookieOrigin);
line fromorg.apache.http.client.protocol.ResponseProcessCookies
in httpClientIn htmlUnit you can create your own strategy and use it in
com.gargoylesoftware.htmlunit.CookieManager
instead ofpublic static final String HTMLUNIT_COOKIE_POLICY = CookiePolicy.BROWSER_COMPATIBILITY; ... final CookieSpec spec = registry_.getCookieSpec(HTMLUNIT_COOKIE_POLICY);
Optionally it should depends from constructor parameter of CookieManager but authors doesn't think so)
/** * HtmlUnit's cookie policy is to be browser-compatible. Code which requires access to * HtmlUnit's cookie policy should use this constant, rather than making assumptions and using * one of the HttpClient {@link CookiePolicy} constants directly. */
So if you want implement your own Cookie strategy or deal with cases where
CookiePolicy.BROWSER_COMPATIBILITY
isn't browser capability you should modify code.

- 15,614
- 21
- 81
- 132