ASP中的ServicePointManager.净的核心

本文关键字:核心 ServicePointManager 中的 ASP | 更新日期: 2023-09-27 18:07:33

我正在尝试将现有的类库代码转换为。net Core类库。在static构造函数中的代码中,我有以下内容:

ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.Expect100Continue = false;

我做了一些搜索和ServicePointManager不再可用在。net核心,WinHttpHandler现在应该使用(ServicePointManager。.net core中的DefaultConnectionLimit ?)。

我的问题是ServicePointManager到底是什么,正在设置的这些属性是什么?

WinHttpHandler不是static作为ServicePointManager,所以我必须创建一个实例来设置这些属性?我需要改变我所有的http调用,以利用该实例吗?

ASP中的ServicePointManager.净的核心

WinHttpHandler继承自HttpMessageHandler,因此您可以在构造HttpClient时将其作为参数传递,如下所示:

WinHttpHandler httpHandler = new WinHttpHandler();
httpHandler.SslProtocols = SslProtocols.Tls12;
HttpClient client = new HttpClient(httpHandler);

希望这对你有帮助!

ServicePointManager从v2.0开始在。net Core中可用

组装System.Net。ServicePoint, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51

也就是说,我已经实现了我的HTTP客户机调用使用:
System.Net.Http.HttpClientHandler handler;  // use DI etc to create 
if (options.SkipSsl)  // config driven
    handler.ServerCertificateCustomValidationCallback = (req, cer, ch, err) => true;

见https://stackoverflow.com/a/44540071/4292717

只需添加以下命名空间:

using System.Net.Http;
using System.Net.Http.Headers;

然后改变你的行如下,

WinHttpHandler httpHandler = new 
WinHttpHandler();
httpHandler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;

代替

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | 
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

快乐编码!!