gpt4 book ai didi

c# - 如何使用 HttpRequestMessage 发送证书?

转载 作者:太空狗 更新时间:2023-10-29 22:30:10 28 4
gpt4 key购买 nike

这里我有一个 HttpRequestMessage,我正在尝试向它添加一个客户端证书,但似乎找不到如何执行此操作。有没有人做过这样的事情?

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "myapi/?myParm=" + aParm);
//Want to add a certificate to request - a .p12 file in my project
myAPIResponse res = await SendAndReadAsAsync<myAPIResponse>(request, aCancelToken);

最佳答案

这是一个结合了 HttpClient 和 HttpRequestMessage 的答案。

保存数据的 HttpRequestMessage 和处理数据发送方式的客户端。

WebRequestHandler handler = new WebRequestHandler();
X509Certificate certificate = GetMyX509Certificate();
handler.ClientCertificates.Add(certificate);
HttpClient client = new HttpClient(handler);
var request = new HttpRequestMessage (HttpMethod.Get, "myapi/?myParm=" + aParm);
HttpResponseMessage response = await client.SendAsync (request);
response.EnsureSuccessStatusCode();

编辑:这里有一个链接解释了 WebRequestHandler、HttpClientHandler 和 HttpClient 之间的区别,以了解您应该在什么时候使用哪一个:https://learn.microsoft.com/en-us/archive/blogs/henrikn/httpclient-httpclienthandler-and-webrequesthandler-explained

关于c# - 如何使用 HttpRequestMessage 发送证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37750881/

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