【问题标题】:C# HTTPS Post with certificate带有证书的 C# HTTPS Post
【发布时间】: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


【解决方案1】:

错误可能有几个原因。这是我的诊断清单。

  1. 使用浏览器(使用 Windows 证书存储区 - IE 或 Chroms)处理 GET 请求,或使用 Fiddler 处理所有其他 HTTP 动词(POST,...)来调用端点。
  2. 确保服务器证书有效(NotBefore 和 NotAfter)并且未被撤销。浏览器将显示错误并提供详细信息。
  3. 检查客户端和服务器匹配的 TLS/SSL 版本。 IISCrypto(名称令人困惑)显示客户端和服务器上的平台配置(Windows SCHANNEL)。此外,还有一个nice PowerShell script 可以打开与所有 TLS/SSL 版本的连接。如果您无权访问服务器,但它是公开可用的,您可以使用 https://www.ssllabs.com/ssltest/analyze.html 检查支持哪些 TLS 版本
  4. 最后但并非最不重要的一点是确保客户端和服务器可以协商密码套件。 IISCryptohttps://www.ssllabs.com/ssltest/analyze.html 也可以显示支持的密码套件。

编辑

用firefox检查,证书没问题,不是自签名(DigiCert):

使用 TLS 1.2 针对提供的 URL 运行 get 请求:

try {
  var urlString = "https://www.viewmyrecord.com/";
  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  var request = (HttpWebRequest) WebRequest.Create(urlString);
  request.ClientCertificates.Add(new X509Certificate());
  request.Method = "GET";

  using (var response = (HttpWebResponse) request.GetResponse()) {
    Console.WriteLine(response.StatusCode);
  }
} catch (Exception ex) {
  Console.ForegroundColor = ConsoleColor.Red;
  Console.WriteLine(ex.ToString());
  Console.ResetColor();
}

从这里开始工作:

【讨论】:

  • 谢谢。我会检查的。
  • 第 3 方说他们只允许公共证书颁发机构 (CA),不接受自签名证书。但是,当我检查我们的 SSL (www.viewmyrecord.com) 时,它显示“自签名”。
  • @MaKristelMirabel 目前状态如何?
  • 嗨,我无法连接到 Surecripts 的电子处方 (smr-staging.surescripts.net/erx{PortalName}/{Version}?id= {MessageID}) 我已经安装了证书(p7b 文件)。这里有人知道带有客户端证书的 HTTPS Post 吗?或者之前有使用过 Surescripts Network 的经验?我真的需要帮助。我的电子邮件地址:kristel@friendlysofttech.com
  • WebRequest 有一个可以设置的属性。看这里docs.microsoft.com/en-us/dotnet/api/…
猜你喜欢
  • 2016-07-09
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 2014-05-05
  • 2020-04-24
相关资源
最近更新 更多