首页 > 编程语言 >C# 调用https接口 安全证书问题 解决方法

C# 调用https接口 安全证书问题 解决方法

时间:2022-12-13 15:55:34浏览次数:70  
标签:CheckValidationResult string 证书 C# System 接口 https Security

原文链接: https://blog.csdn.net/lizaijinsheng/article/details/127321758

说明: 如果是用https的话,由于没有证书,会报错:基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系;

错误信息 : 

1、基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系--安全证书问题 解决方法

2、基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。 ---> System.Security.Authentication.AuthenticationException: 根据验证过程,远程证书无效

3、解决方案

     1 引用命名空间     

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

    2 添加接收方法CheckValidationResult

   public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  { // 总是接受
   return true;
  }

   3 POST方法里面增加调用 : CheckValidationResult

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

   附代码: 

   

public static object KcInterFaces()
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证

//参数拼接,一般参数中间用":"拼接
// string usernamePassword = username + ":" + password;
string url = "路径";
//请求接口需要的参数及拼接过程
//接口需求的令牌
string jsondata = "{ \"RFC_FUNCTION\":\" \",\"REQUEST_CONTEXT\": { ";
// string result = Post_BasicAuth(url, jsondata, username, password);
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Authorization", "认证");
var r1 = MyFramework.Core.OpenApi.HttpWebHelper.Post(url, jsondata, headers);
// //调用方法接收返回结果
//string result = Post_BasicAuth(url, jsondata, usernamePassword);

 

return "";
}

标签:CheckValidationResult,string,证书,C#,System,接口,https,Security
From: https://www.cnblogs.com/fg01/p/16979027.html

相关文章