This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16497|Reply: 5

[Source] .NET/C# multithreaded concurrency using the pit of HttpWebRequest

[Copy link]
Posted on 2021-12-20 15:21:04 | | | |
When doing task scheduling, the task of scheduling is to simulate initiating an HTTP request, and find that when there are more tasks, the task scheduling seems to be like fake death.

The maximum default number of requested connections on a normal Windows system is 2, and on a server operating system, it defaults to 10. If you don't modify this concurrent connection limit,Then the number of http connections that the client can make at the same time is only 2 or 10

System.Net.ServicePointManager.DefaultConnectionLimit This is the maximum number of connections that can get the current default setting, as shown in the figure below:



Is there a default concurrency limit for HttpClient?

Prior to .Net 4.0, Http operations were relied upon by HttpWebRequest. It has a very conservative maximum of 2 concurrency limits for the same site by default, which often causes HttpWebRequests to not get the ideal speed by default (I guess this strategy frustrates many coders), and the value of App.config or ServicePointManager.DefaultConnectionLimit must be modified.

MS introduced an HttpClient class in .Net 4.5 to handle Http operations, and I thought HttpClient and HttpWebRequest followed the same policy. Today, when writing a multi-threaded download program, I used 10 concurrent connections and found that HttpClient does not have a concurrent number limit by default.

At first I thought it was. Net 4.5 removed this concurrency limit (after all, basically no one follows this standard now), and then rewrote the relevant code with WebRequest, and found that it was still 2 concurrency limit, and the value of ServicePointManager.DefaultConnectionLimit was also 2. That is, HttpClient is not controlled by the HttpWebRequest concurrency policy, and there is no system-level concurrency limit.

In addition, during testing, I found that HttpWebRequest does not default to a maximum of 2 concurrency for all addresses. For exampleThere is no concurrency limit for local HTTP address connections (http://localhost/*).

C# multithreaded environment calls to HttpWebRequest concurrent connection limit

.net's HttpWebRequest or WebClient has a concurrent connection limit in the case of multithreading, which is 2 by default on desktop operating systems such as Windows XP and Windows 7, and 10 by default on server operating systems. If you don't change this concurrent connection limit, the number of HTTP connections that a client can make at a time is only 2 or 10. For some applications, such as browsers or spiders, the number of 2 or 10 concurrency is too small and greatly affects the performance of the application. This concurrent connection limit is due to the HTTP 1.0 and HTTP 1.1 standards that specify a maximum of 2 concurrent connections. However, mainstream browsers no longer follow this rule, but the .NET framework still follows this rule by default.

Many articles say that accessing HttpWebRequest asynchronously can improve concurrent performance, but I have tested that if you don't change the default number of concurrent connections, synchronous or asynchronous access performance is not good.

We can also set the maximum number of concurrent connections in app.config as follows:

Reference article

The hyperlink login is visible.
The hyperlink login is visible.





Previous:Xshell Xftp To continue using this program, you must apply the latest...
Next:Maven command pushes third-party jar packages to Nexus private repositories
Posted on 2021-12-26 17:20:57 |
Learn to learn。。。。。
Posted on 2022-1-10 08:42:47 |
Learn to learn。。。。。
Posted on 2022-3-8 15:58:54 |
Learn...
 Landlord| Posted on 2023-2-18 21:47:09 |
In addition, the solution

 Landlord| Posted on 2023-3-10 21:21:15 |
Configure via web.config

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com