如何使用HttpRequestMessage发送证书?

时间:2016-06-10 14:33:56

标签: c# .net ssl cryptography ssl-certificate

这里我有一个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);

1 个答案:

答案 0 :(得分:9)

这是一个将HttpClient与HttpRequestMessage相结合的答案。

HttpRequestMessage持有处理数据发送方式的数据和客户端。

WebRequestHandler handler = new WebRequestHandler();
X509Certificate2 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();