C# Mono - 错误:安全通道失败 - 某些域的 HTTPS 请求失败



通过阅读有关此问题的所有问题数小时后,我别无选择,只能自己发布。 我有一个非常简单的 C# 代码,它从 URL 返回 html 代码:

string url = "https://www.google.fr";
var encoding = Encoding.GetEncoding("iso-8859-1");
using (WebClient client = new WebClient())
{
string html = new StreamReader(client.OpenRead(url), encoding).ReadToEnd();
Console.Write(html);
}

在大多数情况下,此程序会返回结果,但某些导致异常的域除外:

Mono.Security.Protocol.Tls.TlsException: The authentication or decryption has failed.

我尝试了其他问题的所有内容,但没有人可以解决我的问题。我已经尝试像这样覆盖:

ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

问题来自Mono,因为在Mono上不起作用的域在Windows 10上正常工作。 如何避免此异常?

经过几天又一天的搜索解决方案,我明白我必须使用一个技巧,因为错误来自 Mono。

这不是正确的方法,而是最简单的方法。

我使用了一个免费域来托管一个 php 文件,其中包含它domain.com/myfile.php

<?php echo file_get_contents("url of the website"); ?>

然后,我没有使用导致问题的 url,而是用 C# 中的 PHP 文件链接替换。

string url = "domain.com/myfile.php";
using (WebClient client = new WebClient())
{
string html = client.DownloadString(url);
lines = Regex.Split(html, @"r?n|r");
client.Dispose();
}

最新更新