如何将HtmlUnit cookie保存到文件? [英] How to save HtmlUnit cookies to a file?
本文介绍了如何将HtmlUnit cookie保存到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将HtmlUnit cookie保存到文件中,然后在下次运行时加载它们。我怎样才能做到这一点?谢谢。
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.
推荐答案
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();
}
这篇关于如何将HtmlUnit cookie保存到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文