Khalil M Khalil M -4 years ago 241
Java Question

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?

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download