使用 HtmlAgilityPack 的 Web 查询抛出 System.Net.WebException:请求已中止:无法创建 SSL/TLS 安全通道 [重复]

问题描述 投票:0回答:2

从我读到的几个主题中,绝大多数与贝宝有关,还有一些与称为 ServicePointManager 的东西有关。这与其他问题无关!在本例中,我只是尝试一个基本的 html 敏捷包示例,与 paypal 或 ServicePointManger 无关:

        var url = "some_url";
        var web = new HtmlWeb();
      var doc = web.Load(url); 

标记的行会抛出此错误:

Unhandled Exception: System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
   at System.Net.HttpWebRequest.GetResponse()
   at HtmlAgilityPack.HtmlWeb.Get(Uri uri, String method, String path, HtmlDocument doc, IWebProxy proxy, ICredentials creds) in C:\something\HtmlAgilityPack\HtmlAgilityPack.Shared\HtmlWeb.cs:line 1677
   at HtmlAgilityPack.HtmlWeb.LoadUrl(Uri uri, String method, WebProxy proxy, NetworkCredential creds) in C:\Users\Jonathan\source\repos\HtmlAgilityPack\HtmlAgilityPack.Shared\HtmlWeb.cs:line 2086
   at HtmlAgilityPack.HtmlWeb.Load(Uri uri, String method) in C:\something\HtmlAgilityPack\HtmlAgilityPack.Shared\HtmlWeb.cs:line 1300
   at HtmlAgilityPack.HtmlWeb.Load(String url) in C:\something\HtmlAgilityPack\HtmlAgilityPack.Shared\HtmlWeb.cs:line 1197
   at DownloadSeco.Program.Main(String[] args) in C:\something\Program.cs:line 16

到底是什么原因导致此错误以及如何修复它?

编辑:我要结束这个主题,显然问题出在我的特定计算机上,因为它可以在我旁边的电脑上运行。

c# .net ssl html-agility-pack
2个回答
3
投票

据我了解,Paypal 已更新 TLS 版本。详情请看这里

但是.NET默认不支持TLS1.2。

您需要手动启用它。

您可以使用下一个代码来完成:

System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

SecurityProtocol
它是单例。您可以在应用程序中设置一次 TLS(不是针对每个 WebRequest)。您可以在 Program.cs 文件中设置 TLS。

另请查看此 SO topic 了解详细信息。


0
投票

它对我有用,可以使用http,也可以使用https

© www.soinside.com 2019 - 2024. All rights reserved.