中止该请求无法在共享托管服务器C#上创建SSL/TLS安全频道



我们无法使用webrequesthtmlagilitypack连接到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;

如果有人对此有任何想法,请与我们分享。

有时是因为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):rn";
        //The server did not present a certificate
        if ((sslPolicyErrors &
            SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable)
        {
            msg = msg + "rn    -The server did not present a certificate.rn";
            acceptCertificate = false;
        }
        else
        {
            //The certificate does not match the server name
            if ((sslPolicyErrors &
                SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch)
            {
                msg = msg + "rn    -The certificate name does not match the authenticated name.rn";
                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 + "rn    -" + item.StatusInformation;
                        acceptCertificate = false;
                    }
                }
            }
        }
        //If Validation failed, present message box
        if (acceptCertificate == false)
        {
            msg = msg + "rnDo 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();

,如果我们可以看到(代码)您如何制作Web Request。

,它将有助于诊断问题。

最新更新