【发布时间】:2020-02-04 23:27:36
【问题描述】:
我需要帮助在 C# 中使用证书设置 HTTP 帖子。
我收到一个错误:
"The underlying connection was closed: An unexpected error occurred on a send'.
谢谢你。
网络框架是 4.5
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = TrustCertificate;
byte[] bytes = new ASCIIEncoding().GetBytes(transaction);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlString);
request.ClientCertificates.Add(new X509Certificate());
request.Method = "POST";
string str = Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + pwd));
request.Headers["Authorization"] = "Basic " + str;
request.ContentType = "text/xml";
request.ContentLength = bytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string str2 = reader.ReadToEnd();
response.Close();
reader.Close();
return str2;
【问题讨论】:
-
当您将协议修复为 TLS 1.2 时,您确定端点可以处理它吗?
-
我现在收到此错误 System.Net.WebException: The request was aborted: could not create SSL/TLS secure channel。
-
我试图删除 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;并且仍然收到相同的错误。我正在使用 vs 2012
标签: ssl https certificate