13

I'd like to save HtmlUnit cookies to a file and on next run load them from that one. How can I do that? Thanks.

Fluffy
  • 27,504
  • 41
  • 151
  • 234

2 Answers2

24
public static void main(String[] args) throws Exception {
    LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
    
    File file = new File("cookie.file");
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
    Set<Cookie> cookies = (Set<Cookie>) in.readObject();
    in.close();
    
    WebClient wc = new WebClient();
    
    Iterator<Cookie> i = cookies.iterator();
    while (i.hasNext()) {
        wc.getCookieManager().addCookie(i.next());
    }
    
    HtmlPage p = wc.getPage("http://google.com");
    
    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("cookie.file"));
    out.writeObject(wc.getCookieManager().getCookies());
    out.close();
}
Fluffy
  • 27,504
  • 41
  • 151
  • 234
  • 1
    I wish more people commented like you. Simple. Answers the question. Brilliant. Thank you so much! – David Feb 16 '12 at 02:43
  • With htmlunit 2.23 `wc.getCookieManager().addCookie(i.next());` did not cause the Cookies to be sent in the next request (checked the code of _com.gargoylesoftware.htmlunit.Webclient_ and _com.gargoylesoftware.htmlunit.WebRequest_, CookieManager is not used there ). I could only use [this answer](http://stackoverflow.com/a/36155793/2039709) to pass Cookies with a new Request (i.e. building the WebRequest manually) – user2039709 Nov 29 '16 at 11:37
  • `ObjectOutput` is an interface, third statement from end needs to be `ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("cookie.file"));` – mrzzmr Jul 24 '20 at 20:42
3

The above code works only with HtmlUnit (Im not criticizing or anything) i.e. exports only in a format that can be read by HtmlUnit agin.

Here is a more generic one: (This works with curl)

CookieManager CM = WC.getCookieManager(); //WC = Your WebClient's name
    Set<Cookie> set = CM.getCookies();
    for(Cookie tempck : set)    {
        System.out.println("Set-Cookie: " + tempck.getName()+"="+tempck.getValue() + "; " + "path=" + tempck.getPath() + ";");
    }

Now, Make String out of those println(s) in the for loop. write them to a text file.

works with curl :

curl -b "path to the text file" "website you want to visit using the cookie"

-b can be changed with -c too.. check curl docs...

Salman
  • 41
  • 1