限制

HTTP1.1 规定同一域名最多只能同时有两个连接,C# 是在 WebRequest 中实现的。 如果没有设置,则默认只有 2 个并发连接。如果同时下载多个文件,只有 2 个可以同时下载,其他会一直等待。而且等待时间超过超时时间时会报超时异常 The operation has timed out

这是一篇对此问题解释较为详细的博客: System.Net.WebException when issuing more than two concurrent WebRequest’s – Wade Wegner

解决方案

可以完全移除或修改这种限制:

1
2
var request = WebRequest.Create(url) as HttpWebRequest;
request.ServicePoint.ConnectionLimit = 10;

关键点在于一定要使用 WebRequest 自己的 ServicePoint,而不是其他 API。

.net - How can I programmatically remove the 2 connection limit in WebClient - Stack Overflow

解释

I know this is pretty old but I’m putting this here in case it might help somebody else who runs into this issue. We ran into the same problem with parallel outbound HTTPS requests. There are a few issues at play. The first issue is that ServicePointManager.DefaultConnectionLimit did not change the connection limit as far as I can tell. Setting this to 50, creating a new connection, and then checking the connection limit on the service point for the new connection says 2. Setting it on that service point to 50 once appears to work and persist for all connections that will end up going through that service point. The second issue we ran into was with threading. The current implementation of the mono thread pool appears to create at most 2 new threads per second. This is an eternity if you are doing many parallel requests that start at exactly the same time. To counteract this, we tried setting ThreadPool.SetMinThreads to a higher number. It appears that Mono only creates up to 1 new thread when you make this call, regardless of the delta between the current number of threads and the desired number. We were able to work around this by calling SetMinThreads in a loop until the thread pool had the desired number of idle threads. I opened a bug about the latter issue because that’s the one I’m most confident is not working as intended: https://bugzilla.xamarin.com/show_bug.cgi?id=7055 shareimprove this answer

edited Jun 19 ‘13 at 9:58 Bhushan Firake 6,78042562

answered Sep 11 ‘12 at 14:16 Jake Moshenko 26337

c# - HttpWebResponse won’t scale for concurrent outbound requests - Stack Overflow