9

Here I have an HttpRequestMessage, and I am trying to add a client certificate to it, but cannot seem to find how to do this. Has anyone out there done something like this?

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);
KateMak
  • 1,619
  • 6
  • 26
  • 42
  • did you see this? http://stackoverflow.com/questions/22251689/make-https-call-using-httpclient – misha130 Jun 10 '16 at 14:42
  • 1
    Yup, this one appears to be sending it with the HttpClient...I was hoping to see if it is possible to send it with the HttpRequestMessage – KateMak Jun 10 '16 at 14:44

1 Answers1

11

Here is an answer combining HttpClient with HttpRequestMessage.

The HttpRequestMessage holding the data and client handling how the data is sent.

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();

Edit: Here is a link explaining the difference between WebRequestHandler, HttpClientHandler & HttpClient to understand which one you should use when: https://learn.microsoft.com/en-us/archive/blogs/henrikn/httpclient-httpclienthandler-and-webrequesthandler-explained

misha130
  • 5,457
  • 2
  • 29
  • 51