https 的ssl 遇到问题总结

手头项目有个需要,对接第三方接口,第三方接口采用https写的,以前接触到的都是http格式,而今虽有一个字母之差,但是用起来还是有不少需要注意的地方。

同样的代码,开始运行告诉我连接意外关闭,很是苦恼,自己不断尝试,屡次不成,但是最后一次运行时候成功了,这是不解,为啥同样代码会出现两种不同结果,先将代码粘贴出来,供大家解析:

 

 {
    protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;

                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                //ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
                //ServicePointManager.ServerCertificateValidationCallback = new   System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                X509Certificate objx509 = new X509Certificate(@"E:\file123.cer");
                ICMangerServices.NciicServices cilent = new ICMangerServices.NciicServices();

                //ICMangerServices.NciicServices ics = new ICMangerServices.NciicServices();
                //ics.SoapVersion = System.Web.Services.Protocols.SoapProtocolVersion.Soap12;
                cilent.ClientCertificates.Add(objx509);

                string resut = cilent.nciicGetCondition("ssssss");
                string ddt = cilent.nciicCheck("sjdifsjdifjsdifsdfas", "asdfasdfasdfa");
            }
            catch (Exception ex)
            { 
            
            }
        } 

        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        { // Always accept 
            return true;
        }

        private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
    }


    internal class AcceptAllCertificatePolicy : ICertificatePolicy
    {
        public AcceptAllCertificatePolicy()
        {
        }
        public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert, WebRequest wRequest, int certProb)
        {
            return true;
        }
    }


你可能感兴趣的:(Web学习)