C#调用webapi HTTPS报错:基础连接已经关闭- 未能为 SSL-TLS 安全通道创建 ...

打印 上一主题 下一主题

主题 226|帖子 226|积分 678

1、起首参加定名空间:
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;

  SSL网站,连接时必要提供证书,对于非必须提供客户端证书的环境,只要返回一个安全确认即可。我的是.NET FrameWork4.0
  2、参加以下代码:
  1. public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  2.     {
  3.         //直接确认,否则打不开
  4.         return true;
  5.     }
复制代码
3、接收证书进行身份验证ssl,在调用api接口前调用此方法:
  ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  以下是完整案例:
  1. public string HttpPost(string url, string body)
  2.     {
  3.         Encoding encoding = Encoding.UTF8;
  4.         string jsonText = string.Empty;
  5.         string dataText1 = string.Empty;
  6.         if (string.IsNullOrEmpty(url.Trim()))
  7.         {
  8.             return "";
  9.         }
  10.         //接收证书进行身份验证
  11.         ServicePointManager.ServerCertificateValidationCallback =
  12.             new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  13.         //ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
  14.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  15.         request.Method = "POST";
  16.         request.Accept = "text/plain, */*; q=0.01";
  17.         request.ContentType = "application/json;charset=utf-8";
  18.         byte[] buffer = encoding.GetBytes(body);
  19.         request.ContentLength = buffer.Length;
  20.         request.GetRequestStream().Write(buffer, 0, buffer.Length);
  21.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  22.         using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  23.         {
  24.             jsonText = reader.ReadToEnd();
  25.             dataText1 = Regex.Replace(jsonText, @"\", "");
  26.         }
  27.         return dataText1;
  28.     }
复制代码
调用:
dlbzUrl是调用地址 https://…/api/tddbzzljcxt/zybrxx/vnoentry-query
model.FCYRQSTART 开始时间
model.FCYRQEND 结束时间
  1.   string DaliBaiHospitalJson = HttpPost(dlbzUrl, "{"FIDATES":"" + model.FCYRQSTART + " 00:00:00" + "","FIDATEE":"" + model.FCYRQEND + " 23:59:59" + ""}");
复制代码
学习网络安全技能的方法无非三种:
  第一种是报网络安全专业,现在叫网络空间安全专业,主要专业课程:步伐计划、计算机组成原理原理、数据结构、操纵体系原理、数据库体系、 计算机网络、人工智能、自然语言处理、社管帐算、网络安全法律法规、网络安全、内容安全、数字取证、机器学习,多媒体技能,信息检索、舆情分析等。
  第二种是自学,就是在网上找资源、找教程,大概是想办法认识一-些大佬,抱紧大腿,不过这种方法很耗时间,而且学习没有规划,大概很长一段时间感觉本身没有进步,容易劝退。
  假如你对网络安全入门感爱好,那么你必要的话可以点击这里

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

诗林

高级会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表