How to save HtmlUnit cookies to a file?

前端 未结 2 727
眼角桃花
眼角桃花 2020-12-30 09:56

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.

2条回答
  •  死守一世寂寞
    2020-12-30 10:05

    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 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...

提交回复
热议问题