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(""), null);

But on GAE it throws:

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

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