5

I'm looking for a way to accept cookies (all cookies) using HTMLUnit

I'm trying to login to my wordpress site using HTMLUnit but I can't submit the form (and therefor can not login) when the cookies are rejected as my error is:

Jul 14, 2012 10:42:24 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies WARNING: Cookie rejected:

CODE:

package backend;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Set;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;

public class temp {

    WebClient myClient = new WebClient(BrowserVersion.FIREFOX_3);
    CookieManager cm = new CookieManager();
    HtmlPage page;

    public void Someting() throws FailingHttpStatusCodeException, MalformedURLException, IOException{

    //Disabling Javascript for now.
    myClient.setJavaScriptEnabled(false);
    myClient.setCssEnabled(false);
    myClient.setCookieManager(cm);

    page = myClient.getPage("http://nick.wordpress.com/wp-admin");

    Set<Cookie> cookies = myClient.getCookieManager().getCookies();

    System.out.println("Page status code: " + page.getWebResponse().getStatusCode() + "\nPage status message: "  + page.getWebResponse().getStatusMessage());

    if(cookies != null)
    {
        for(Cookie cookie : cookies)
        {
            this.myClient.getCookieManager().addCookie(cookie);
        }
    }

    System.out.println("We have: " + myClient.getCookieManager().getCookies().size() + " cookie");
    }

    public static void main(String args[]) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
        temp t = new temp();
        t.Someting();
    }
}

This code is excluding the WordPress form filling/submitting function as this doesn't seem relevant.

When searching the web I could only find 1 or 2 vague answers where I should modify the "http.client.protocol.ResponseProcessCookies" class but I hope it can be done without this.

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
user1372760
  • 89
  • 1
  • 5
  • Maybe this discussion will help. Htmlunit might not expose the concerned configuration though. http://stackoverflow.com/questions/7459279/httpclient-warning-cookie-rejected-illegal-domain-attribute – Paddy Jul 16 '14 at 20:25

1 Answers1

0

You question is answered here

You have to recreate the CookieManager class. Find the method public synchronized Set<Cookie> getCookies(final URL url) and remove the statement if (spec.match(cookie, cookieOrigin)).

Community
  • 1
  • 1
Yiannis Tsimalis
  • 739
  • 2
  • 11
  • 23