zoukankan      html  css  js  c++  java
  • 解决HttpWebRequest访问https请求被中止: 未能创建 SSL/TLS 安全通道

    处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)只需加上以下两行代码就行了。//处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)ServicePointManager.ServerCertificateValidation…

    处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)

    只需加上以下两行代码就行了。

    //处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)
    ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    

    以上代码是.Net 4.5以上版本可以直接使用。

    .Net 4.0版本使用下面代码

    ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;

    示例:

        /*发送微信订阅通知消息*/
            private string sendWxNotify(string name,string tel,int goodsId, HttpContext context)
            {
                ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    
                HttpWebRequest requet = (HttpWebRequest)WebRequest.Create("https://xx/postmessage?key=ad&name="+name+"&tel="+tel+"&id="+goodsId.ToString());
                requet.Method = "GET";
               
                using (HttpWebResponse response = (HttpWebResponse)requet.GetResponse()) 
                {
                    StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.Default);
                  return  sr.ReadToEnd();
                }
                return "";
            }

    来源:http://geobook.net/news/show-1097.html

  • 相关阅读:
    袁创:如何成为黄金程序猿
    划重点!新版电子病历评级标准讲解会上6大核心要点
    台湾医院信息化见闻录
    2500行代码实现高性能数值表达式引擎
    HIT创业感言:只有长寿的企业才有持续价值
    袁创:寂静的战争
    相约南湖,南京都昌信息亮相南湖HIT论坛
    我们是谁?南京都昌信息科技有限公司!
    医疗链的系列谈 第一篇 基本概念研究
    论电子病历控件的现状和发展方向
  • 原文地址:https://www.cnblogs.com/fogwang/p/14462711.html
Copyright © 2011-2022 走看看