How to post data using HttpClient?
149,240
Solution 1
You need to use:
await client.PostAsync(uri, content);
Something like that:
var comment = "hello world";
var questionId = 1;
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("comment", comment),
new KeyValuePair<string, string>("questionId", questionId)
});
var myHttpClient = new HttpClient();
var response = await myHttpClient.PostAsync(uri.ToString(), formContent);
And if you need to get the response after post, you should use:
var stringContent = await response.Content.ReadAsStringAsync();
Hope it helps ;)
Solution 2
Try to use this:
using (var handler = new HttpClientHandler() { CookieContainer = new CookieContainer() })
{
using (var client = new HttpClient(handler) { BaseAddress = new Uri("site.com") })
{
//add parameters on request
var body = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("test", "test"),
new KeyValuePair<string, string>("test1", "test1")
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "site.com");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded; charset=UTF-8"));
client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
client.DefaultRequestHeaders.Add("X-MicrosoftAjax", "Delta=true");
//client.DefaultRequestHeaders.Add("Accept", "*/*");
client.Timeout = TimeSpan.FromMilliseconds(10000);
var res = await client.PostAsync("", new FormUrlEncodedContent(body));
if (res.IsSuccessStatusCode)
{
var exec = await res.Content.ReadAsStringAsync();
Console.WriteLine(exec);
}
}
}
Related videos on Youtube
Author by
user2970840
Updated on April 07, 2020Comments
-
user2970840 about 4 years
I have got this HttpClient from Nuget.
When I want to get data I do it this way:
var response = await httpClient.GetAsync(url); var data = await response.Content.ReadAsStringAsync();
But the problem is that I don't know how to post data? I have to send a post request and send these values inside it:
comment="hello world"
andquestionId = 1
. these can be a class's properties, I don't know.Update I don't know how to add those values to
HttpContent
as post method needs it.httClient.Post(string, HttpContent);
-
Patrick over 10 yearsDid you try to use the Post method?
-
Patrick over 10 yearsYou should follow the documentation for what content you should send in your post (if you are following an API). Then, just fill a HttpContent and use PostAsync did you try that?
-
user2970840 over 10 years@Patrick thanks, I have updated my question
-
Patrick over 10 yearsBtw, posting comments 10 minutes after posting your question with "can't anyone help?" and a smiley face will probably not encourage other overflowers to help you faster. If you don't find anyone answering your question you might want to look at your question and see what you can do to improve it, with more information about what you tried, instead of expecting everyone else to guess what you know.
-
user2970840 over 10 years@Patrick ok, I have updated it. please see if that is enough.
-
RBT over 4 yearsIf someone is looking to upload a file using HttpClient - C# HttpClient 4.5 multipart/form-data upload
-
-
user2970840 over 10 yearsthanks, but I think this
HttpClient
is better thanWebClient
. easier and cleaner. isn't it? -
user2970840 over 10 yearsresponse is
Unprocessable entry
. maybe I have a mistake somewhere else -
Lori Lalonde - MSFT over 10 yearsAh yes, I'm so used to WebClient, I had that in my head when I was reading the question. I haven't used HttpClient as of yet. Sorry!
-
hkarask about 7 yearsor shorter dictionary literal:
var formContent = new FormUrlEncodedContent(new Dictionary<string, string> { {"comment", comment}, {"questionId", questionId } });
-
ed22 about 3 yearsYou may want to use MultipartFormDataContent instead of FormUrlEncodedContent (that depends on what your server expects)
-
RBT over 2 yearsRelated post - POSTing JsonObject With HttpClient From Web API