Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 views
in Technique[技术] by (71.8m points)

.net - Maximum concurrent requests for WebClient, HttpWebRequest, and HttpClient

Are there any limit to how many concurrent/parallel requests you can send using WebClient, HttpWebRequest, and HttpClient class in .NET 4.5? Or is it that you can send unlimited parallel requests if there is no restriction on the web API or server? Sorry if the question is too broad, please correct me so I can narrow it down.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, there is a limit. The default connection limit is 2 concurrent connections per remote host. This can be overridden. For example, I believe that ASP.NET by default overrides the default to be 10 connections per remote host.

From https://msdn.microsoft.com/en-us/library/7af54za5.aspx:

The number of connections between a client and server can have a dramatic impact on application throughput. By default, an application using the HttpWebRequest class uses a maximum of two persistent connections to a given server, but you can set the maximum number of connections on a per-application basis.

To change the connection limit yourself, use ServicePointManager.DefaultConnectionLimit. Make sure to make this change soon after your process starts up, before you start making HTTP connections to the remote host. This is because because once you actually make an HTTP connection to a remote host, then a ServicePoint will be created for that remote host using the current default connection limit.

Note that there are other effective limits on concurrent connections beyond what's enforced by the HTTP client classes. For example, if you wanted to open a million concurrent connections, you'd probably run out of RAM, or threads, or some other OS resource. If you're bumping up against those limits, then you'll need to ask another more general question about how to scale up a .net process to process a lot of concurrent I/O. But if you're only opening a few tens of connections then you should be fine.

BTW, if you're curious why the default limits are so low, it's actually baked into the HTTP specification-- the goal was to ensure that selfish HTTP clients didn't swamp HTTP servers with simultaneous connections. Although modern browsers ignore the spec. See http://www.stevesouders.com/blog/2008/03/20/roundup-on-parallel-connections/ for a lot more detail about this topic than you ever wanted to know!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...