Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set WebClient Content-Type Header?

To conect to a third party service I need to make a Https Post. One of the requisites set is to sent a custom content type.

I'm using WebClient, but I can't find how to set it. I've tried making a new class and overriding the CreateRequest Method, but that make request crash.

Is there any way to do that without having to rewrite CopyHeadersTo method?

EDIT CopyHeaderTo is a method I've seen using .NET Reflector. It's invoked from GetWebRequest and sets all Request Headers, including Content-Type, from private properties.

like image 810
MaLKaV_eS Avatar asked Oct 20 '09 16:10

MaLKaV_eS


People also ask

What is a header in WebClient?

The Headers property contains a WebHeaderCollection instance containing protocol headers that the WebClient sends with the request. Some common headers are considered restricted and are protected by the system and cannot be set or changed in a WebHeaderCollection object.

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.


1 Answers

You could try adding to the Headers collection.

myWebClient.Headers.Add("Content-Type","application/xxx"); 
like image 97
Darin Dimitrov Avatar answered Oct 05 '22 15:10

Darin Dimitrov