如何将 HtmlUnit cookies 保存到文件中?

发布于 2024-08-21 18:43:49 字数 67 浏览 4 评论 0原文

我想将 HtmlUnit cookies 保存到一个文件中,并在下次运行时从该文件加载它们。我怎样才能做到这一点?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-08-28 18:43:49
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();
}
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();
}
太阳男子 2024-08-28 18:43:49

上面的代码仅适用于 HtmlUnit(我不是在批评什么)
即仅以 HtmlUnit 可以读取的格式导出。

这是一个更通用的:(这适用于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() + ";");
    }

现在,从for循环中的那些println中生成字符串。将它们写入文本文件。

与curl一起使用:

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

-b也可以用-c更改..检查curl文档...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文