I want to replace WebClient to HttpClient in my code. What HttpContent I have to use in HttpClient to replace WebClient.UploadString? My WebClient code:
string data = string.Format("name={0}&warehouse={1}&address={2}", name, shop.Warehouse.Id, shop.Address);
using (var wc = new WebClient()) {
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.Encoding = Encoding.UTF8;
string fullUrl = BaseUrl + url;
string response = wc.UploadString(fullUrl, data);
// ...
}
You can construct your postdata and use it in the instance of the FormUrlEncodedContent like so:
// This is the postdata
var data = new List<KeyValuePair<string, string>>();
data.Add(new KeyValuePair<string, string>("Name", "test"));
HttpContent content = new FormUrlEncodedContent(data);
There are solutions specified on this page:
How to use System.Net.HttpClient to post a complex type?
You can decide on posting it asynchronously or synchronously for example:
HttpResponseMessage x = await httpClient.PostAsync(fullUrl, content);