当通过HttpWebRequest调用第三方系统接口时,如果是https,会出现“请求被中止: 未能创建 SSL/TLS 安全通道”。
HttpWebRequest对象需要针对https做对应的处理,部分示例代码如下:
//创建httpWebRequest对象
HttpWebRequest httpRequest = null;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
| SecurityProtocolType.Tls
| (SecurityProtocolType)0x300 //Tls11
| (SecurityProtocolType)0xC00; //Tls12
httpRequest = WebRequest.Create(url) as HttpWebRequest;
httpRequest.ProtocolVersion = HttpVersion.Version10;
}
else
{
httpRequest = WebRequest.Create(url) as HttpWebRequest;
}
推荐阅读