请求被中止: 未能创建 SSL/TLS 安全通道。
问题描述: 今天用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"; httpRequest.Timeout = timeout * 1000; //填充并发送要post的内容 httpRequest.ContentLength = data.Length; using (Stream requestStream = httpRequest.GetRequestStream()) { requestStream.Write(data, 0, data.Length); requestStream.Close(); } //发送post请求到服务器并读取服务器返回信息 var response = httpRequest.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { //读取服务器返回信息 string stringResponse = string.Empty; using (StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8)) { stringResponse = responseReader.ReadToEnd(); } responseStream.Close(); return stringResponse; } }
已经这么写了,但是偶尔还是会出现这个异常
解决没有啊
请求被中止: 未能创建 SSL/TLS 安全通道。
问题描述: 今天用HttpWebRequest类Post请求https的接口,报错:请求被中止: 未能创建 SSL/TLS 安全通道。解决方法: 参考如下函数,...
点击下载文档
上一篇:常用的星空环境判断下一篇:财务:应付单上面的已结算金额是怎么反写的?
本文2024-09-16 17:19:50发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-15005.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章