Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you add a Certificate to WebClient (C#)?

I know it is pretty simple to add a certificate to a HttpWebRequest. However, I have not found a way to do the equivalent using WebClient. Basically, I want to send out a POST with a specific certificate using WebClient.

How would you accomplish this exact code using WebClient:

var request = (HttpWebRequest) WebRequest.Create("my-url"); request.Method = "POST"; request.ClientCertificates.Add(new X509Certificate()); //add cert 
like image 671
Andrew Avatar asked Jan 14 '10 18:01

Andrew


People also ask

How do you post data on WebClient?

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest. RegisterPrefix method. UploadString Sends a String to the resource and returns a String containing any response.

How do you send parameters data using WebClient POST request in C?

var url = "https://your-url.tld/expecting-a-post.aspx" var client = new WebClient(); var method = "POST"; // If your endpoint expects a GET then do it. var parameters = new NameValueCollection(); parameters. Add("parameter1", "Hello world"); parameters. Add("parameter2", "www.stopbyte.com"); parameters.

What is WebClient class?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.


1 Answers

You must subclass and override one or more functions.

class MyWebClient : WebClient {     protected override WebRequest GetWebRequest(Uri address)     {         HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);         request.ClientCertificates.Add(new X509Certificate());         return request;     } } 
like image 180
Mikael Svenson Avatar answered Sep 20 '22 07:09

Mikael Svenson