Multithreaded WebClient requests return error - System.Net.WebException

前端 未结 1 1884
一个人的身影
一个人的身影 2021-01-29 04:42

I have 5000+ pages I want to download using WebClient. Since I want that done as fast as possible I am trying to use multithreading (using BlockingCollection<

1条回答
  •  情歌与酒
    2021-01-29 05:24

    The class is not guaranteed to be thread safe. from MSDN:

    Any instance members are not guaranteed to be thread safe

    Update

    Use one HttpWebRequest for each request that you make. If you make a lot of requests to different web sites it doesn't matter if you use WebClient or HttpWebRequest.

    If you do a lot of requests to the same web site it is still not as inefficient as it seems. HttpWebRequest reuse connections (it's hidden underneath the hood). Microsoft uses something called service points and you can access them through the HttpWebRequest.ServicePoint property. If you click on the property definition you come to the ServicePoint documentation where you can fine tune the number of connections per web site etc.

    0 讨论(0)
提交回复
热议问题