本文介绍了htmlunit中的Cookie政策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在htmlunit中指定Cookie政策以接受所有Cookie?

How do you specify a cookie policy in htmlunit to accept all cookies?

推荐答案

只需重新创建整个CookieManager类:
这是类的源:

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

现在查找此方法 public synchronized Set< Cookie> getCookies(final URL url)
在这里你找到:

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

远程规范匹配语句 if(spec.match(cookie,cookieOrigin))你应该接受所有的cookies,不管策略。和/或你可以处理ACCEPT_ALL_COOKIES标志,并通过匹配的规格,如果这是配置中指示的政策。

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.

这篇关于htmlunit中的Cookie政策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 05:55