使用 WatiN 获取设置的 cookie 列表

发布于 2024-11-10 00:00:41 字数 426 浏览 3 评论 0原文

有没有办法使用 WatiN 获取网站设置的所有 cookie 列表?

WatiN 中的 IE 浏览器类提供了一个 GetCookie 方法,允许您检索特定的 cookie,但我想迭代所有已设置的 cookie。

有两种方法可以让您获取 cookie:

CookieCollection cookies = _browser.GetCookiesForUrl(new Uri(url));

CookieContainer cookies = _browser.GetCookieContainerForUrl(new Uri(url) );

但这两个都是空的。调用特定 cookie 的 GetCookie 方法也会返回 null。

关于如何让它发挥作用有什么建议吗?

Is there a way to get a list of all the cookies set by a website using WatiN?

The IE Browser class in WatiN provides a GetCookie method that allows you to retrieve a specific cookie, but I would like to iterate over all the cookies that have been set.

There are two methods that should allow you to get the cookies:

CookieCollection cookies = _browser.GetCookiesForUrl(new Uri(url));

and

CookieContainer cookies = _browser.GetCookieContainerForUrl(new Uri(url));

But both of these are empty. Also calling the GetCookie method for a specific cookie returns null.

Any suggestions of how to get this to work?

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

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

发布评论

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

评论(2

心不设防 2024-11-17 00:00:41

最近我不得不处理这种情况。起初我以为我要找的cookie是HttpOnly,但是我用WireShark看了一下,没有HttpOnly标志。

不知道为什么 GetCookieContainerForUrl 在这种情况下会失败,但客户端脚本调用显示 cookie 仍然存在:

ie.Eval("document.cookie");

您可能希望在每次使用数据包嗅探之前尝试该语句。

Recently I had to deal with this situation. At first I thought the cookies I was looking for were HttpOnly, but I took a look using WireShark and there was no HttpOnly flag.

Not sure why GetCookieContainerForUrl fails in this case, but a client side script call revealed the cookies were still there:

ie.Eval("document.cookie");

You might want to try that statement before resorting to packet sniffing every time.

南…巷孤猫 2024-11-17 00:00:41

好吧,我想这些方法应该按预期工作,但也许您正在尝试获取 HttpOnly cookie?许多站点/Web 框架为重要的 cookie 设置此标志,特别是当涉及“会话 id”cookie 时。你无法在 WatiN 中阅读它们,而且真的很难阅读它们。我一直在寻找解决方案,只有一篇文章: Retrieve HttpOnly Session Cookie in WebBrowser< /a>

如果您想知道您尝试获取 cookie 的网站是否在 cookie 上设置了 HttpOnly 标志,请使用 Fiddler2 并查看响应标头。

Well, I suppose those methods should work as expected, but maybe you are trying to get HttpOnly cookies? Many sites/web frameworks sets this flag for important cookies, especially when it comes to "session id" cookies. You can't read them in WatiN and it's really hard to read them at all. I was looking for solution once and only one I got was article: Retrieve HttpOnly Session Cookie in WebBrowser

If you want to know if the site you are trying to get cookies is setting HttpOnly flag on the cookie, use Fiddler2 and look in response headers.

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