我目前正在与第 3 方创建的系统集成。该系统要求我使用 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
:
基础连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。
更新因为它是我正在使用的测试服务器,所以证书不受信任并且验证失败......要在测试/调试环境中绕过这个,创建一个新的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;
}