C# 请求被中止。无法在共享宿主服务器C上创建ssl/tls安全通道#
我们无法使用C# 请求被中止。无法在共享宿主服务器C上创建ssl/tls安全通道#,c#,asp.net,asp.net-mvc,httpwebrequest,html-agility-pack,C#,Asp.net,Asp.net Mvc,Httpwebrequest,Html Agility Pack,我们无法使用webrequest或htmlagilitypack连接到https服务器,显示以下错误 基础连接已关闭:receive.System.Net.WebException发生意外错误:或无法在服务器上创建SSL/TLS安全通道 我们的代码在localhost上运行良好,我们还在代码文件中添加了以下部分,但我们无法确定为什么它只在服务器上发生 ServicePointManager.Expect100Continue = true; ServicePointManager.Securi
webrequest
或htmlagilitypack
连接到https服务器,显示以下错误
基础连接已关闭:receive.System.Net.WebException发生意外错误:
或无法在服务器上创建SSL/TLS安全通道
我们的代码在localhost上运行良好,我们还在代码文件中添加了以下部分,但我们无法确定为什么它只在服务器上发生
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
如果有人对此有任何想法,请与我们分享。有时是因为webrequest不接受自签名证书。我有一个我通常使用的单亲班。它接受所有自签名证书。如果您共享了您试图访问的url,则更容易确定是否有更简单的解决方案
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;
}
}
只需在执行类似的web请求之前调用该方法
Certificates.Instance.GetCertificatesAutomatically();
此外,如果我们能够看到(代码)您是如何提出webrequest的,那么它将有助于诊断问题