从我读到的几个主题中,绝大多数与 Paypal 有关,还有一些与称为 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
到底是什么原因导致此错误以及如何修复它?
编辑:我要结束这个主题,显然问题出在我的特定计算机上,因为它可以在我旁边的电脑上运行。
最佳答案
据我了解,Paypal 已更新 TLS 版本。详情为here .
但是.NET默认不支持TLS1.2。
您需要手动启用它。
您可以使用下面的代码来做到这一点:
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
SecurityProtocol
它是单例。您可以在应用程序中设置一次 TLS(不是针对每个 WebRequest)。您可以在 Program.cs 文件中设置 TLS。
另请查看此 SO topic了解详情。
关于c# - 使用 HtmlAgilityPack 的 Web 查询抛出 System.Net.WebException : The request was aborted: Could not create SSL/TLS secure channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51668380/