In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how C # uses HttpClient. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Incorrect use of New HttpClient
As in the following code, call http is often used in daily development, new one HttpClient at a time
Using (var client = new HttpClient ())
First, let's do a simple test, loop call a url, the code is as follows
Public async Task GetXXXUrlAsync (string url) {var html = ""; for (var I = 0; I
< 20; i++) { using (var client = new System.Net.Http.HttpClient()) { var result=await client.GetStringAsync(url); html += result; } } return html; } 运行项目输出结果后,通过 netstate 查看下 TCP 连接情况 C:\Windows\System32>Netstat
Although the project has finished running, the connection still exists with the status of "TIME_WAIT" (continue to wait to see if any delayed packets will be transmitted; by default, under windows, the TIME_WAIT state will keep the connection for 240s. In the case of high concurrency, there is no time to release the connection, socket is exhausted, and a favorite error occurs after exhaustion.
The reason for the error is that the resources occupied by the object should be released in a timely manner, but this is wrong for network connections for the following reasons:
Network connection takes a certain amount of time. Frequently opening and closing the connection will affect the performance.
Opening a network connection will consume the underlying socket resources, but when HttpClient calls its own Dispose method, it can not immediately release the underlying socket resources, which means that the program may cause catastrophic problems by running out of connection resources.
For the above error, you may think of HttpClient that uses static singleton mode, and then move on
Static HttpClientprivate static HttpClient Client = new HttpClient ()
Although static singleton mode can solve the above problem, it will bring another problem: DNS changes will result in unparsable, DNS will not be reloaded, and need to be restarted to change. (this demonstration is relatively negative and complex, so those who are interested can give it a try.)
Correct use
HttpClientFactory rebuilds the way HttpClient is used in a modular, namable, configurable, and flexible manner, with the DI framework injected into the IHttpClientFactory factory, which creates the HttpClient and requests the Handler from the internal Handler pool allocation.
The HttpClient created by HttpClientFactory, that is, HttpClientHandler, is just that these HttpClient are put into the "pool", and the factory automatically determines whether to create or reuse each time it is in create (the default life cycle is 2min).
Net core 2.1began to introduce the IHttpClientFactory factory class to automatically manage the creation and resource release of the IHttpClientFactory class, which can be used through Ioc injection
Services.AddControllers (); services.AddHttpClient ()
IHttpClientFactory is used. The abbreviated code is as follows
Private readonly IHttpClientFactory _ clientFactory;public HomeController (IHttpClientFactory clientFactory) {_ clientFactory = clientFactory;} public async Task GetBaiduAsync (string url) {var client = _ clientFactory.CreateClient (); var result = await client.GetStringAsync (url); return result;} Thank you for reading! This is the end of the article on "how to use HttpClient in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.