Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
NameValueCollection _params = new NameValueCollection();

_params.Add("Request[title]", "عرض جديد شركة محمد النهدى العقارية");
_params.Add("Request[description]", "شقة بحى البندرية عمارة اللؤلؤة");
_params.Add("Request[type]", "شقة");
_params.Add("Request[request_type]", "FOR_RENT");
_params.Add("Request[price_from]", "20000");
_params.Add("Request[price_to]", "35000");
_params.Add("Request[area_from]", "100");
_params.Add("Request[area_to]", "150");


string sPostURL = "https://test.com/api/saveRequest";

var wbClient = new WebClient();

wbClient.Headers.Add("Content-Type", "application/form-data");

wbClient.Headers.Add("token", "9665840");

var response = wbClient.UploadValues(sPostURL, "Post", _params);

Error Messege
The Content-Type header cannot be changed from its default value for this request.'


What I have tried:

Error Messege <pre>The Content-Type header cannot be changed from its default value for this request.'
Posted
Updated 17-Apr-20 13:34pm

1 solution

First thing to note is that WebClient is kinda old and is not recommended for new development, instead you should be using HttpClient
Important
We don't recommend that you use the WebClient class for new development. Instead, use the System.Net.Http.HttpClient class.
References:
WebClient Class (System.Net) | Microsoft Docs[^]
HttpClient Class (System.Net.Http) | Microsoft Docs[^]

The second to note is that no matter which client you are using, it is only the transit mechanism to send the HTTP Request and to receive the HTTP Response.
What you need to do is to set the header the actual request; and not on the client handling the request.
Microsoft has a good sample for this as well, complete with setting the headers
How to: Send data by using the WebRequest class | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900