使用相同的 CookieContainer 进行冲浪
如何为每个 Web 请求分配相同的 CookieContainer 来浏览网站?
How can you surf on a website assigning the same CookieContainer to each web request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我几年前写的一堂课。 它并不完全完整,并且是在我完全理解一切如何工作之前完成的(例如,它不能正确编码复杂的 POST 数据),但它确实可以很好地解决所有缺陷,并且它将演示如何保留 cookie 容器。 它也在 VB.Net 中,但您可以将其构建到单独的程序集中,或者如果需要,可以通过翻译器运行它:
轻微更新的 C# 版本是 现已在 GitHub 上,包括更新的用户代理。 它也不太可能仅仅接受异常。
This is a class I wrote a few years back. It's not quite complete and was done before I fully understood how everything works (It doesn't properly encode complex POST data, for example), but it does work pretty well for the all the flaws and it will demonstrate how you can keep your cookiecontainer. It's also in VB.Net, but you can just build that into a separate assembly or run it through a translator if you need to:
A lightly-updated C# version is now on GitHub, including a more-recent User Agent. It's also less likely to just swallow exceptions.
CookieContainer 的设计就像浏览器 cookie 存储一样。 因此它可以在任何站点中包含 cookie,因为它将处理域路径以及过期时间。 我可以看出,同一个容器中可以有任何域,因此任何 Web 请求也应该可以有相同的容器。
您应该注意到 CookieContainer 在 .Add(Cookie) 和 .GetCookies(uri) 方法上有一个错误。
请在此处查看详细信息并修复:
http ://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
CookieContainer is designed just like browser cookie store. So it can contain cookies in any sites because it will handle the domain path as well as expiry. I can tell that it possible to have any domain in the same container, so any web request should also possible to have the same container.
You should notice that CookieContainer has a bug on .Add(Cookie) and .GetCookies(uri) method.
See the details and fix here:
http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html