
```csharp
问题描述: 今天用HttpWebRequest类Post请求https的接口,报错:请求被中止: 未能创建 SSL/TLS 安全通道。
解决方法: 参考如下函数,
在创建HttpWebRequest实例之前,加上 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
public static string PostData(byte[] data, string url, string contentType = "application/json", int timeout = 20)
{
//创建httpWebRequest对象
HttpWebRequest httpRequest = null;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
httpRequest = WebRequest.Create(url) as HttpWebRequest;
httpRequest.ProtocolVersion = HttpVersion.Version10;
}
else
{
httpRequest = WebRequest.Create(url) as HttpWebRequest;
}
if (httpRequest == null)
{
throw new ApplicationException(string.Format("Invalid url string: {0}", url));
}
//填充httpWebRequest的基本信息
httpRequest.ContentType = contentType;
httpRequest.Method = "POST";