“ServicePointManager.FindServicePoint(Uri)”已过时:改用 HttpClient

Abh*_*hra 5 httpclient servicepointmanager c#-6.0

更新 C# 从 NET 5 升级到 6 后开始出现此错误 -

警告 SYSLIB0014“ServicePointManager.FindServicePoint(Uri)”已过时:“WebRequest、HttpWebRequest、ServicePoint 和 WebClient 已过时。请改用 HttpClient。

var servicePoint = ServicePointManager.FindServicePoint(requestUri.GetEndpoint());
            if (servicePoint.ConnectionLeaseTimeout == -1){}
Run Code Online (Sandbox Code Playgroud)

Pau*_*ado 3

.NET 6 的网络重大更改中,WebRequest、WebClient 和 ServicePoint 已过时:

WebRequest、WebClient 和 ServicePoint 已过时

System.Net.WebRequest、System.Net.WebClient 和 System.Net.ServicePoint 类被标记为已过时,并SYSLIB0014在编译时生成警告。

版本介绍

6.0

更改说明

WebRequest、WebClient 和 ServicePoint 类已添加到 2.0 版本中的 .NET Core 中,以实现向后兼容性。但是,他们引入了一些运行时破坏性更改,例如,WebRequest.GetRequestStream为整个响应分配内存,并且WebClient.CancelAsync并不总是立即取消。

从 .NET 6 开始,WebRequest、WebClient 和 ServicePoint 类已弃用。这些课程仍然可用,但不建议用于新开发。为了减少分析器警告的数量,仅使用 ObsoleteAttribute 属性修饰构造方法。

建议操作

请改用 System.Net.Http.HttpClient 类。

对于FTP,由于HttpClient不支持,我们建议使用第三方库。

受影响的API

  • 网络请求
  • HttpWeb请求
  • FtpWeb请求
  • 网络客户端
  • 服务点

  • 它只是说使用 HttpClient 类来代替,但仅此而已。还需要发生什么? (4认同)