gpt4 book ai didi

c# - 使用 HttpWebRequest 和 X509 SSL 证书检查 HTTPS Web 服务是否可用

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:00 36 4
gpt4 key购买 nike

我需要实现一种检查 Web 服务是否可用的方法。主要问题是 URL 是 HTTPS 并且需要证书。所以我需要从机器上获取证书,然后创建一个 HttpWebRequest 来获取一个 HttpWebResponse

通过我的实际实现,我可以检索证书,但是当我调用 request.GetResponse() 方法时,它会抛出此异常:

[System.Net.WebException] = {"The underlying connection was closed: An unexpected error occurred on a send"}

内部异常:

[System.IO.IOException] = {"Received an unexpected EOF or 0 bytes from the transport stream"}

我阅读了许多可能的解决方案,但似乎没有一个适合我。我正在使用 .NET 3.5

这是我的代码

using (WebClient client = new WebClient())
{
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindByThumbprint, CertificateThumbprint, false);

if (certificates.Count == 0)
throw new Exception("No corresponding certificate found.");

X509Certificate2 certificate = certificates[0];

// Not compiling on .NET 3.5
// Error: cannot apply '+=' operator to
//'System.Net.Security.RemoteCertificateValidationCallback'
//and 'anonymous method'
//ServicePointManager.ServerCertificateValidationCallback +=
// delegate(
// object sender,
// X509Certificate certificate,
// X509Chain chain,
// SslPolicyErrors sslPolicyErrors)
// {
// return true;
// };

// Not compiling on .NET 3.5
// Error: cannot apply '+=' operator to 'System.Net.Security.RemoteCertificateValidationCallback' and 'lambda expression'
// ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

// Not working
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

// Not working
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UrlWSInvioComunicazioni);
request.ClientCertificates.Add(certificate);
request.Method = "HEAD";

// throws Exception...
HttpWebResponse webResponse = request.GetResponse() as HttpWebResponse;
webResponse.Close();

response.ResultCode = ResultCode.Success;
response.ResultMessage = "Service available.";
}

更新 2017/05/10

因为我使用的是不支持 TLS 1.2 的 .NET 3.5,所以我使用了 Microsoft 的补丁(参见 KB3154519 ),它可以通过使用 SecurityProtocolTypeExtensions 来使用 TLS 1.2和 SslProtocolsExtensions 扩展类。

自 2017 年 1 月(当 Microsoft 发布补丁时)以来,我已经将该方法用于其他一些网络服务,并且它没有任何问题。

我认为我现在遇到的问题:

[System.Net.WebException] = {"The request was aborted: Could not create SSL/TLS secure channel."}

与我电脑中的某些东西有关。

更新 2017/05/12

我终于发现问题是与IIS有关。我 PC 上的 Web 应用程序的应用程序池正在运行,身份设置为 ApplicationPoolIdentity。当设置为 LocalSystem 或我当前的用户(管理员)时,它起作用了。

所以我认为当应用程序池作为 ApplicationPoolIdentity 运行时,访问证书的某些东西是不正确的。

最佳答案

扫描 URL 以查看服务器使用的 SSL 提供商。你可以在这里做 -> https://www.ssllabs.com/ssltest/analyze.html

protocols

出于安全原因,某些服务器禁用了 ssl3。所以你必须改用这个设置:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;

或者如果客户端可用,则使用 Tls 1.2。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls12;

更新检查此代码是否适合您

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)3072;
var httpRequest = (HttpWebRequest)WebRequest.Create("https://google.com");
httpRequest.Method = "GET";
httpRequest.ContentType = "text/html";

httpRequest.KeepAlive = false;
httpRequest.MaximumAutomaticRedirections = 30;

var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
var responseStream = new StreamReader(httpResponse.GetResponseStream());

var strResponse = responseStream.ReadToEnd();

httpResponse.Close();
responseStream.Close();

关于c# - 使用 HttpWebRequest 和 X509 SSL 证书检查 HTTPS Web 服务是否可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43864687/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com