请求被中止 无法在共享托管服务器 C# 上创建 ssl/tls 安全通道
Posted
技术标签:
【中文标题】请求被中止 无法在共享托管服务器 C# 上创建 ssl/tls 安全通道【英文标题】:the request was aborted could not create ssl/tls secure channel on shared hosting server C# 【发布时间】:2018-05-09 01:27:33 【问题描述】:我们无法使用webrequest
或htmlagilitypack
连接到 https 服务器它显示以下错误
The underlying connection was closed: An unexpected error occurred on a receive.System.Net.WebException:
或 could not create SSL/TLS secure channel on server
我们的代码在 localhost 上运行良好,我们还在我的代码文件中添加了以下部分,但我们无法确定它为什么只在服务器上发生。
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
如果有人对此有任何想法,请与我们分享。
【问题讨论】:
【参考方案1】:有时是因为 webrequest 不接受自签名证书。我有这个我通常使用的单例类。它接受所有自签名证书。如果您共享了您尝试访问的网址,则更容易确定是否有更简单的解决方案。
public sealed class Certificates
private static Certificates instance = null;
private static readonly object padlock = new object();
Certificates()
public static Certificates Instance
get
lock (padlock)
if (instance == null)
instance = new Certificates();
return instance;
public void GetCertificatesAutomatically()
ServicePointManager.ServerCertificateValidationCallback +=
new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors)
=> return true; );
private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
//Return true if the server certificate is ok
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
bool acceptCertificate = true;
string msg = "The server could not be validated for the following reason(s):\r\n";
//The server did not present a certificate
if ((sslPolicyErrors &
SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable)
msg = msg + "\r\n -The server did not present a certificate.\r\n";
acceptCertificate = false;
else
//The certificate does not match the server name
if ((sslPolicyErrors &
SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch)
msg = msg + "\r\n -The certificate name does not match the authenticated name.\r\n";
acceptCertificate = false;
//There is some other problem with the certificate
if ((sslPolicyErrors &
SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.RemoteCertificateChainErrors)
foreach (X509ChainStatus item in chain.ChainStatus)
if (item.Status != X509ChainStatusFlags.RevocationStatusUnknown &&
item.Status != X509ChainStatusFlags.OfflineRevocation)
break;
if (item.Status != X509ChainStatusFlags.NoError)
msg = msg + "\r\n -" + item.StatusInformation;
acceptCertificate = false;
//If Validation failed, present message box
if (acceptCertificate == false)
msg = msg + "\r\nDo you wish to override the security check?";
// if (MessageBox.Show(msg, "Security Alert: Server could not be validated",
// MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
acceptCertificate = true;
return acceptCertificate;
在发出这样的网络请求之前调用该方法。
Certificates.Instance.GetCertificatesAutomatically();
如果我们可以看到(代码)您如何进行网络请求,这将有助于诊断问题。
【讨论】:
以上是关于请求被中止 无法在共享托管服务器 C# 上创建 ssl/tls 安全通道的主要内容,如果未能解决你的问题,请参考以下文章
MSBuild 还原给出“请求被中止:无法创建 SSL/TLS 安全通道。”
Api 随机调用失败 - 请求被中止:无法创建 SSL/TLS 安全通道