繁体   English   中英

请求被中止无法在共享主机服务器C#上创建ssl / tls安全通道

[英]the request was aborted could not create ssl/tls secure channel on shared hosting server C#

我们无法使用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):\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();

如果我们能够看到(代码)您如何进行网络请求,它还有助于诊断问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM