gpt4 book ai didi

c# - Postman http 请求成功,C# HttpWebRequest 错误 SSL/TLS 安全通道的信任关系

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:40 26 4
gpt4 key购买 nike

相关文章但没有解决问题:


总结

尝试使用基本 HttpWebRequest 在 C# 中请求时,返回错误:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel., System.Security.Authentication.A uthenticationException: The remote certificate is invalid according to the validation procedure.

但是使用Postman向第三方API请求时,返回成功。


实际代码:

Console.WriteLine("---START---");

var url = "https://" + ConfigurationManager.AppSettings["ClientDNS"].ToString() + ConfigurationManager.AppSettings["ClientTokenUri"].ToString();
var redirect = ConfigurationManager.AppSettings["UserRedirect"].ToString();
var clientId = ConfigurationManager.AppSettings["ClientId"].ToString();
var code = ConfigurationManager.AppSettings["ClientCode"].ToString();
var result = "";

Console.WriteLine(string.Format("url : {0}\n", url));
Console.WriteLine(string.Format("redirect : {0}\n", redirect));
Console.WriteLine(string.Format("clientid : {0}\n", clientId));
Console.WriteLine(string.Format("code : {0}\n", code));

try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Tls13
| SecurityProtocolType.Ssl3;

//As suggested by Ali Bahrami
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

var postData = "grant_type=authorization_code&redirect_uri=" + redirect + "&code=" + code + "&client_id=" + clientId;

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Update base from link 01
httpWebRequest.Method = "POST";
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.Timeout = 20 * 1000;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";

byte[] buffer = Encoding.Default.GetBytes(postData);
if (buffer != null)
{
httpWebRequest.ContentLength = buffer.Length;
httpWebRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
}

Console.WriteLine("getting response");
var response = (HttpWebResponse)httpWebRequest.GetResponse();
result = string.Format("result: {0}\n", new StreamReader(response.GetResponseStream()).ReadToEnd());

}

catch (Exception ex)
{
result = string.Format("result: {0}\n", ex.Message + (ex.InnerException != null ? ", " + ex.InnerException : ""));
}

when using C# code

when using Postman

链接 01 - https://stackoverflow.com/a/41970776/8975971

最佳答案

我认为由于使用自签名 证书,ServicePointManagerServer 无法验证您的证书。正如我在评论中建议的那样,您需要编写一种方法来更改您的案例中的这种行为。

解决方法之一是在验证发生时返回 true:

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

当然,在实际情况下,有些人不赞成这种解决方法,因为您实际上禁用了证书验证。但是,如果您正在处理内部 Web 服务,只需使用上述方法忽略验证即可。

关于c# - Postman http 请求成功,C# HttpWebRequest 错误 SSL/TLS 安全通道的信任关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56536435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com