c# - 为什么我在使用线程的并发 HTTP 连接上受限

标签 c# multithreading http iis-10

我正在尝试同时打开大约 25 个到主机的连接。
我的操作系统是 windows 10。
为了进行测试,我在本地 IIS 上打开了一个简单的网站,并使用 Thread.Sleep(2000) 向用户延迟 2 秒响应一个简单的数据。

现在在客户端使用这段代码:

const int len = 25;
for (int i = 0; i < len; i++)
{
    new Thread(new ParameterizedThreadStart((idx) =>
    {
        // start downloading data.
        var res = new WebClient().DownloadString("http://192.168.1.101:8090/");

        // log index and time when done.
        Console.WriteLine($"{Convert.ToInt32(idx).ToString("00")} done at:{ DateTime.Now.ToString("HH:mm:ss:ffff") }");
    })).Start(i);
}

我得到了以下结果:

Thread 01 done at 40:8476 ms
Thread 00 done at 40:8476 ms
Thread 03 done at 40:8496 ms
Thread 04 done at 40:8496 ms
Thread 02 done at 40:8506 ms
Thread 05 done at 40:8506 ms
Thread 07 done at 40:8516 ms
Thread 06 done at 40:8516 ms
Thread 08 done at 40:8536 ms
Thread 09 done at 40:8545 ms
Thread 11 done at 42:8510 ms
Thread 10 done at 42:8510 ms
Thread 12 done at 42:8560 ms
Thread 14 done at 42:8560 ms
Thread 15 done at 42:8570 ms
Thread 13 done at 42:8580 ms
Thread 16 done at 42:8590 ms
Thread 17 done at 42:8590 ms
Thread 18 done at 42:8610 ms
Thread 19 done at 42:8610 ms
Thread 21 done at 44:8565 ms
Thread 20 done at 44:8565 ms
Thread 23 done at 44:8634 ms
Thread 24 done at 44:8654 ms
Thread 22 done at 44:8654 ms

以上结果告诉我们:
1- Thread 0 to 9 同时获取数据。(秒40)
2- 线程 10 到 19 在上一步后 2 秒后同时获取数据。(第 42 秒)
3- Thread 20 to 24 同时拿到了数据。上一步后 2 秒。(秒 44)

现在我的问题是限制了我,为什么它只能同时打开 10 个 HTTP 连接,我该如何设置它无限制。
如果有任何其他平台或编程语言,将受到欢迎。

最佳答案

谁?您的操作系统和 Web 服务器制造商,即 Microsoft。

为什么?因为 Windows 10 是客户端操作系统,Microsoft 不希望您在其上托管任何严肃的 Web 应用程序。

例如:

来自While using signalr, will there be any connection limits on IIS , 链接到 the SignalR documentation :

When SignalR is hosted in IIS, the following versions are supported. Note that if a client operating system is used, such as for development (Windows 8 or Windows 7), full versions of IIS or Cassini should not be used, since there will be a limit of 10 simultaneous connections imposed, which will be reached very quickly since connections are transient, frequently re-established, and are not disposed immediately upon no longer being used. IIS Express should be used on client operating systems.

还有其他帖子,比如Does windows 10 connection limit apply to self-hosted applications? ,提到 20 个连接,但那是 20 个设备(可能由远程 IP 地址识别)连接到选定的 Windows 服务(SMB、IIS 等)。

IIS 限制为 10,我认为自 Windows 7 以来已经存在多年。

所以这个问题的前半部分答案是:

  • 如果您在 Windows 10 上开发时需要超过 10 个同时传入 HTTP 连接,请使用 IIS Express 或 IIS 或 Cassini 以外的任何其他网络服务器。

但是这个答案还有后半部分。如果您需要超过 10 个(或 2 个,或...,取决于环境)同时传出 HTTP 连接,ServicePointManager 可以管理它。见:

因此在执行请求之前更改限制:

System.Net.ServicePointManager.DefaultConnectionLimit = 25;

关于c# - 为什么我在使用线程的并发 HTTP 连接上受限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58306129/

相关文章:

c# - DirectoryInfo.EnumerateFiles(...) 导致 UnauthorizedAccessException(和其他异常)

c# - 为什么这个方法调用不明确?

asp.net - 浏览器何时删除 session cookie?

C++ - 不同线程通知的 std::condition_variable

vb.net - 如何知道线程中的工作何时完成?

python - 如何在 Python 中发出 PATCH 请求?

http - 每个请求的 Apache HTTP 客户端 4.3 凭据

c# - 验证另一个字符串之间的字符串?

c# - 在Crystal报表的报表标题中提供动态名称

python - Google App Engine (GAE) 中的线程或后台进程