Search code examples
javagoogle-app-enginecookieshtmlunit

How to add a cookie to HtmlUnit webclient on GAE


I want to add a cookie to a webclient, this code works as expected outside GAE:

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

But on GAE it throws:

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)

How can I add this cookie on GAE?


Solution

  • There was a GAE bug about this, possibly you should create a new one there, since it the root cause still exists.

    To get around it, you can use:

    CookieManager cookieManager = new CookieManager() {
        protected int getPort(URL url) {
    
            // or deduct it from url.getProtocol()
            return 80;
        }  
    };
    webClient.setCookieManager(cookieManager);