如何在 GAE 上向 HtmlUnit webclient 添加 cookie

How to add a cookie to HtmlUnit webclient on GAE

我想向网络客户端添加 cookie,此代码在 GAE 之外按预期工作:

WebClient webClient = new WebClient(CHROME);
webClient.addCookie("storepath=us/en", new URL("http://www.zara.com/"), null);

但是在 GAE 上它抛出:

java.lang.IllegalArgumentException: Port may not be negative
    at org.apache.http.util.Args.notNegative(Args.java:115)
    at org.apache.http.cookie.CookieOrigin.<init>(CookieOrigin.java:52)
    at com.gargoylesoftware.htmlunit.CookieManager.buildCookieOrigin(CookieManager.java:102)
    at com.gargoylesoftware.htmlunit.WebClient.addCookie(WebClient.java:2258)

如何在 GAE 上添加此 cookie?

有一个关于这个的GAE bug,也许你应该在那里创建一个新的,因为它的根本原因仍然存在。

要绕过它,您可以使用:

CookieManager cookieManager = new CookieManager() {
    protected int getPort(URL url) {

        // or deduct it from url.getProtocol()
        return 80;
    }  
};
webClient.setCookieManager(cookieManager);