Lor*_*iot 7 c# ssl ssl-certificate servicepoint dotnet-httpclient
我正在 WinForms 中重写一些 Web 处理代码并从 HttpWebRequest 切换到 HttpClient。我需要的最后一件事我似乎无法找到如何完成。
在 HttpWebRequest 中,我可以从我连接的 Web 服务器捕获证书并显示它:
...
HttpWebRequest request = CreateHttpRequest(destUri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cert = request.ServicePoint.Certificate;
if (cert != null)
{
cert2 = new X509Certificate2(cert);
X509Certificate2UI.DisplayCertificate(cert2);
}
...
Run Code Online (Sandbox Code Playgroud)
我找不到使用 HttpClient 捕获证书的等效方法:
//... Use HttpClient.
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(destUri))
{
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里如何/在哪里可以做到这一点?我不知道如何访问 ServicePoint.Certificate。
显然,您不需要从 ServicePointManager.ServerCertificateValidationCallback 获取证书。您可以从 ServicepointManager 本身找到它,如下所示:
//... Use HttpClient.
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(destUri))
{
// Get Certificate Here
var cert = ServicePointManager.FindServicePoint(destUri).Certificate;
//
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5858 次 |
| 最近记录: |