如何删除cookies?
我想以编程方式删除 cookie。
更具体地说,我使用 HtmlUnit 和 Java 来自动化浏览器操作。执行某些操作后,我想清除 cookie,以便后续操作有意义。我如何通过 HtmlUnit 或通过 Java 或通过任何其他方式自动清除 cookie。
I want to delete cookies programmatically.
More specifically, I am using HtmlUnit with Java to automate browser operations. After performing some operations I want to clear cookies so that my subsequent operations make sense. How can I clear cookies through HtmlUnit or through Java or through any other way automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 CookieManager 吗?
此类似乎具有清除所有 cookie 或单独删除它们的方法。
Can you use the CookieManager?
This class appears to have methods for clearing all cookies or removing them individually.
实际上,HtmlUnit 中的 cookie 在以编程方式保存之前不会存储在磁盘上。他们留在记忆中。当 HtmlUnit 会话过期时,它们会从内存中删除。
In actual, the cookies in HtmlUnit doesn't get stored on disk until saved programmatically. They remain in the memory. From the memory they gets deleted when the HtmlUnit session expires.
您可以使用 JavaScript 代码来使 Cookie 过期,使用 Now 立即使其过期
document.cookie = name + "=; " + Now + "; path=/";
创建 cookie 的语法
document.cookie = "name=value; expires=date; path=path;
域=域;安全的”;
You can user javascript code to expire your cookie using Now to expire it immediately
document.cookie = name + "=; " + Now + "; path=/";
syntax for creating cookie
document.cookie = "name=value; expires=date; path=path;
domain=domain; secure";