它是安全使用在多个HttpWebRequests相同的CookieContainer? [英] Is it safe to use the same CookieContainer across multiple HttpWebRequests?

查看:330
本文介绍了它是安全使用在多个HttpWebRequests相同的CookieContainer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一种WebCrawler的,而我需要坚持请求之间的饼干状态。

I am doing a kind of WebCrawler and I need to persist the Cookies state between requests.

我下载的所有页面异步创建新HttpWebRequest的实例,但设置相同的CookieContainer。该页面可以写和读的cookie。

I download all pages async creating new HttpWebRequest instances, but setting the same CookieContainer. The pages can write and read cookies.

我可以安全的做法?还有就是isn't子类的CookieContainer,把锁在所有的方法任何替代?

Can I do it safely? There is any alternative that isn´t subclass the CookieContainer and put locks at all method?

在MSDN说,这种类isn't线程安全的,但在实践中,可以我这样做呢?

The MSDN says that this class isn´t thread safe, but in practice, can I do it?

推荐答案

据该的文档

任何公共静态(共享在这种类型的Visual Basic中)成员都是
线程安全的。任何实例成员不能保证是线程
是安全的。

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

所以,你应该确保正确锁紧,如果你想分享多个线程之间的相同的实例。但由于各成员在的CookieContainer 类实际上是不是被你的代码操作,但含蓄地从您创建它可能不容易正确地同步,比其他不同的HttpWebRequest实例当然,锁定您的要求,其中当然那种失败的目的和平行度,我想你正试图在这里实现的水平。

So, you should ensure proper locking if you want to share the same instance between multiple threads. But since the members of the CookieContainer class are actually not manipulated by your code, but implicitly from the different HttpWebRequest instances that you created it could not be easy to synchronize properly, other than of course locking on your requests which of course kind of defeats the purpose and the level of parallelism that I suppose you are trying to achieve here.

无论是在实践中你会得到问题是另一个话题。问题是,文档(因此笔者)没有提供你任何保证。

Whether in practice you will get problems is another topic. The thing is that the documentation (and thus the author) doesn't provide you any guarantees.

这篇关于它是安全使用在多个HttpWebRequests相同的CookieContainer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆