简体   繁体   English

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

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

We are not able to connect to https server using webrequest or htmlagilitypack It showing below error 我们无法使用webrequesthtmlagilitypack连接到https服务器它显示以下错误

The underlying connection was closed: An unexpected error occurred on a receive.System.Net.WebException: or could not create SSL/TLS secure channel on server The underlying connection was closed: An unexpected error occurred on a receive.System.Net.WebException:could not create SSL/TLS secure channel on server

Our code works fine on localhost and we also added following portion in my code file but we aren't able to identify why it's happening only on the server. 我们的代码在localhost上工作正常,我们还在我的代码文件中添加了以下部分,但我们无法确定它仅在服务器上发生的原因。

ServicePointManager.Expect100Continue = true;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

if anyone has any idea on this then please share with us. 如果有人对此有任何想法,请与我们分享。

Sometimes it is because the webrequest wont accept self signed certificates. 有时这是因为webrequest不接受自签名证书。 I have this singleton class I usually use. 我有这个我经常使用的单身课程。 It accepts all self signed certificates. 它接受所有自签名证书。 It would be easier to determine if there is a simpler solution if if you shared the url you are trying to access. 如果您共享了您尝试访问的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;
    }

}

Just call the method before doing a web request like so. 只需在执行Web请求之前调用该方法。

Certificates.Instance.GetCertificatesAutomatically();

Also it would help diagnose the problem if we could see(the code) how you're making your webrequest. 如果我们能够看到(代码)您如何进行网络请求,它还有助于诊断问题。

暂无
暂无

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

相关问题 服务器错误:请求已中止:无法创建SSL / TLS安全通道 - Server Error: The request was aborted: Could not create SSL/TLS secure channel C# Web 请求 w RestSharp - “请求被中止:无法创建 SSL/TLS 安全通道” - C# Web Request w RestSharp - "The request was aborted: Could not create SSL/TLS secure channel" 具有X509Certificate2的C#HttpClient-WebException:请求已中止:无法创建SSL / TLS安全通道 - C# HttpClient with X509Certificate2 - WebException: The request was aborted: Could not create SSL/TLS secure channel C# Windows 服务,请求被中止:无法使用 SecurityProtocol 创建 SSL/TLS 安全通道 - C# Windows Service ,The request was aborted: Could not create SSL/TLS secure channel with SecurityProtocol C#Dynamics CRM Online“请求已中止:无法创建SSL / TLS安全通道。” - C# Dynamics CRM Online “The request was aborted: Could not create SSL/TLS secure channel.” 请求被中止:无法创建 SSL/TLS 安全通道 c# Webrequest - The request was aborted: Could not create SSL/TLS secure channel c# Webrequest 请求被中止无法创建 SSL/TLS 安全通道 webClient C# - the request was aborted Could not a create SSL/TLS secure channel webClient C# C# 错误。 请求被中止:无法创建 SSL/TLS 安全通道 - C# error. The request was aborted: Could not create SSL/TLS secure channel C# 控制台应用程序 - 请求被中止:无法使用 Restsharp 创建 SSL/TLS 安全通道 - C# Console application - The request was aborted: Could not create SSL/TLS secure channel using Restsharp C# - RestSharp - 请求被中止:无法创建 SSL/TLS 安全通道 - C# - RestSharp - The request was aborted: Could not create SSL/TLS secure channel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM