gpt4 book ai didi

c# - WebClient + HTTPS 问题

转载 作者:IT王子 更新时间:2023-10-29 03:44:30 26 4
gpt4 key购买 nike

我目前正在与第三方创建的系统集成。该系统要求我使用 XML/HTTPS 发送请求。第 3 方将证书发给我,我安装了它

我使用以下代码:

using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "text/xml");

System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
var response = client.UploadData(address, "POST", encoding.GetBytes(msg));
}

此代码返回以下 WebException:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

更新 因为它是我正在使用的测试服务器,所以证书不受信任并且验证失败...要在测试/调试环境中绕过它,请创建一个新的 ServerCertificateValidationCallback

ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(bypassAllCertificateStuff);

这是我的“假”回调

private static bool bypassAllCertificateStuff(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
{
return true;
}

阅读更多 herehere

最佳答案

允许所有证书的代码的最短符号实际上是:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

并且可以很好地解决这个错误。不用说,您应该提供一个实际检查证书并根据证书信息决定通信是否安全的实现。出于测试目的,请使用上面的代码行。

关于c# - WebClient + HTTPS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/536352/

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