HttpWebRequest和HttpClient

HttpWebRequest和HttpClient

本文介绍了WebClient,HttpWebRequest和HttpClient的最大并发请求数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.NET 4.5中的WebClient,HttpWebRequest和HttpClient类可以发送多少并发/并行请求是否有限制?或者,如果对Web API或服务器没有限制,您是否可以发送无限制的并行请求?
很抱歉,如果问题太广泛,请纠正我,以便缩小范围。

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.

推荐答案

是的,那里是一个限制。默认连接限制是每个远程主机2个并发连接。这可以被覆盖。例如,我相信ASP.NET默认会将默认值覆盖为每个远程主机10个连接。

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.

来自:

要自己更改连接限制,请使用。在开始建立与远程主机的HTTP连接之前,请确保在进程启动后立即进行此更改。这是因为一旦你实际建立到远程主机的HTTP连接,那么。

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.

请注意,除了HTTP客户端类强制执行的操作之外,还有其他有效的并发连接限制。例如,如果要打开一百万个并发连接,则可能会耗尽RAM,线程或其他一些OS资源。如果您遇到这些限制,那么您将需要提出另一个更普遍的问题,即如何扩展.net进程以处理大量并发I / O.但是,如果你只打开几十个连接,那么你应该没问题。

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,如果你很好奇为什么默认限制如此之低,它实际上已经融入了HTTP规范 - 目标是确保自私的HTTP客户端没有' t swamp HTTP服务器同时连接。虽然现代浏览器忽略了规范。请参见有关此主题的详细信息比您想知道的更多!

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!

这篇关于WebClient,HttpWebRequest和HttpClient的最大并发请求数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 02:12