How to post data using HttpClient?

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, … Read more

HttpClient – A task was cancelled?

There’s 2 likely reasons that a TaskCanceledException would be thrown: Something called Cancel() on the CancellationTokenSource associated with the cancellation token before the task completed. The request timed out, i.e. didn’t complete within the timespan you specified on HttpClient.Timeout. My guess is it was a timeout. (If it was an explicit cancellation, you probably would … Read more

Adding Http Headers to HttpClient

Create a HttpRequestMessage, set the Method to GET, set your headers and then use SendAsync instead of GetAsync. var client = new HttpClient(); var request = new HttpRequestMessage() { RequestUri = new Uri(“http://www.someURI.com”), Method = HttpMethod.Get, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(“text/plain”)); var task = client.SendAsync(request) .ContinueWith((taskwithmsg) => { var response = taskwithmsg.Result; var jsonTask = response.Content.ReadAsAsync<JsonObject>(); jsonTask.Wait(); … Read more

Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry)

It is matter of ServicePoint. Which provides connection management for HTTP connections. The default maximum number of concurrent connections allowed by a ServicePoint object is 2. So if you need to increase it you can use ServicePointManager.DefaultConnectionLimit property. Just check the link in MSDN there you can see a sample. And set the value you … Read more