我想将 HtmlUnit cookie 保存到一个文件中,并在下次运行时从该文件加载它们。我怎样才能做到这一点?谢谢。
最佳答案
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();
}
关于java - 如何将 HtmlUnit cookie 保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2237286/