C# Mono - Error: SecureChannelFailure - HTTPS request fails for some domain
C# Mono - Error: SecureChannelFailure - HTTPS request fails for some domain
几个小时后,通过阅读有关此问题的所有问题,我别无选择,只能自己发帖。
我有一个非常简单的 C# 代码,其中 return html 来自 URL 的代码:
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);
}
在大多数情况下,这个程序 return 一个结果,除了一些导致异常的域:
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 上不起作用的域在 Windows10 上可以正常工作。
我怎样才能避免这个异常?
经过几天又一天的搜索解决方案,我明白我必须使用一个技巧,因为错误来自 Mono。
这不是正确的方法,但最简单。
我使用了一个免费域来托管一个 php 文件,该文件包含在
domain.com/myfile.php
:
<?php echo file_get_contents("url of the website"); ?>
然后,我没有使用导致问题的 url,而是用 C# 中 PHP 文件的 link 替换。
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();
}
几个小时后,通过阅读有关此问题的所有问题,我别无选择,只能自己发帖。 我有一个非常简单的 C# 代码,其中 return html 来自 URL 的代码:
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);
}
在大多数情况下,这个程序 return 一个结果,除了一些导致异常的域:
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 上不起作用的域在 Windows10 上可以正常工作。 我怎样才能避免这个异常?
经过几天又一天的搜索解决方案,我明白我必须使用一个技巧,因为错误来自 Mono。
这不是正确的方法,但最简单。
我使用了一个免费域来托管一个 php 文件,该文件包含在
domain.com/myfile.php
:
<?php echo file_get_contents("url of the website"); ?>
然后,我没有使用导致问题的 url,而是用 C# 中 PHP 文件的 link 替换。
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();
}