zoukankan      html  css  js  c++  java
  • 未能创建 SSL/TLS 安全通道解决方案

    操作

    当向一个https的url上发送请求,报错:未能创建 SSL/TLS 安全通道;

    报错前实现代码如下:

    /// <summary>
    /// 判断远程文件是否存在
    /// </summary>
    /// <param name="fileUrl">文件路径</param>
    /// <returns></returns>
    public ActionResult FileExists(string fileUrl)
    {
    var result = new ResultModel();
    HttpWebRequest re = null;
    HttpWebResponse res = null;
    try
    {
    
    re = (HttpWebRequest)WebRequest.Create(fileUrl);
    res = (HttpWebResponse)re.GetResponse();
    if (res.ContentLength != 0)
    {
    result.Success = true;
    }
    }
    catch (Exception ex)
    {
    
    result.Success = false;
    result.Message =  ex.Message;
    
    }
    finally
    {
    if (re != null)
    {
    re.Abort();//销毁关闭连接
    }
    if (res != null)
    {
    res.Close();//销毁关闭响应
    }
    }
    return Json(result, JsonRequestBehavior.AllowGet);
    }

    原因:

    ssl证书不受信任,验证失败;

    解决方案:

    1:先加入命名空间

    using System.Net.Security;    
    using System.Security.Authentication;
    using System.Security.Cryptography.X509Certificates;

    2:再重载CheckValidationResult方法,返回true

    public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)         {

                //直接确认,否则打不开    

                return true;

            }

    3:然后在HttpWebRequest req =(HttpWebRequest) WebRequest.Create(URL)前面加上如下一行代码

    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验

    最终实现代码如下:

      /// <summary>
            /// 判断远程文件是否存在
            /// </summary>
            /// <param name="fileUrl">文件路径</param>
            /// <returns></returns>
            public ActionResult FileExists(string fileUrl)
            {
                var result = new ResultModel();
                HttpWebRequest re = null;
                HttpWebResponse res = null;
                try
                {
                    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                    re = (HttpWebRequest)WebRequest.Create(fileUrl);
                    res = (HttpWebResponse)re.GetResponse();
                    if (res.ContentLength != 0)
                    {
                        result.Success = true;
                    }
                }
                catch (Exception ex)
                {
    
                    result.Success = false;
                    result.Message =ex.Message;
    
                }
                finally
                {
                    if (re != null)
                    {
                        re.Abort();//销毁关闭连接
                    }
                    if (res != null)
                    {
                        res.Close();//销毁关闭响应
                    }
                }
                return Json(result, JsonRequestBehavior.AllowGet);
            }
    

      

  • 相关阅读:
    NHibernate之映射文件配置说明
    JS jQuery json日期格式问题的办法
    Spring.net 配置说明
    NHibernate常见问题及解决方法
    Could not load type 'System.Web.Mvc.ViewPage<dynamic>' in asp.net mvc2 after publishing the website
    启用SQLite的Data Provider 运行WECOMPANYSITE时遇到ERROR CREATING CONTEXT 'SPRING.ROOT': ERROR THROWN BY A DEPENDENCY OF OBJECT 'SYSTEM.DATA.SQLITE'
    Nuget 命令 NuGet 管理项目库
    vs2013(vs2015) 打开vs2010 找不到此项目类型所基于的应用程序 MVC2 升级 MVC5 不能加载Web项目
    JsonResult作为Action返回值时的错误
    MVC 数据验证
  • 原文地址:https://www.cnblogs.com/wu-peng/p/10156397.html
Copyright © 2011-2022 走看看