Adding Http Headers to HttpClient

I need to add http headers to the HttpClient before I send a request to a web service. How do I do that for an individual request (as opposed to on the HttpClient to all future requests)? I’m not sure if this is even possible. var client = new HttpClient(); var task = client.GetAsync(“http://www.someURI.com”) .ContinueWith((taskwithmsg) … Read more

Custom header to HttpClient request

How do I add a custom header to a HttpClient request? I am using PostAsJsonAsync method to post the JSON. The custom header that I would need to be added is “X-Version: 1” This is what I have done so far: using (var client = new HttpClient()) { client.BaseAddress = new Uri(“https://api.clickatell.com/”); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Authorization = … Read more

Adding headers when using httpClient.GetAsync

I’m implementing an API made by other colleagues with Apiary.io, in a Windows Store app project. They show this example of a method I have to implement: var baseAddress = new Uri(“https://private-a8014-xxxxxx.apiary-mock.com/”); using (var httpClient = new HttpClient{ BaseAddress = baseAddress }) { using (var response = await httpClient.GetAsync(“user/list{?organizationId}”)) { string responseData = await response.Content.ReadAsStringAsync(); … Read more

HttpClient – A task was cancelled?

It works fine when have one or two tasks however throws an error “A task was cancelled” when we have more than one task listed. List<Task> allTasks = new List<Task>(); allTasks.Add(….); allTasks.Add(….); Task.WaitAll(allTasks.ToArray(), configuration.CancellationToken); private static Task<T> HttpClientSendAsync<T>(string url, object data, HttpMethod method, string contentType, CancellationToken token) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, url); HttpClient … Read more

HttpClient not supporting PostAsJsonAsync method C#

I am trying to call a web API from my web application. I am using .Net 4.5 and while writing the code I am getting the error HttpClient does not contain a definition PostAsJsonAsync method. Below is the code: HttpClient client = new HttpClient(); client.BaseAddress = new Uri(“http://localhost:51093/”); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue(“application/json”)); var user = new … Read more

How do I set up HttpContent for my HttpClient PostAsync second parameter?

public static async Task<string> GetData(string url, string data) { UriBuilder fullUri = new UriBuilder(url); if (!string.IsNullOrEmpty(data)) fullUri.Query = data; HttpClient client = new HttpClient(); HttpResponseMessage response = await client.PostAsync(new Uri(url), /*expects HttpContent*/); response.Content.Headers.ContentType = new MediaTypeHeaderValue(“application/json”); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); return responseBody; } The PostAsync takes another parameter that needs to be HttpContent. … Read more

HttpClient.GetAsync(…) never returns when using await/async

Edit: This question looks like it might be the same problem, but has no responses… Edit: In test case 5 the task appears to be stuck in WaitingForActivation state. I’ve encountered some odd behaviour using the System.Net.Http.HttpClient in .NET 4.5 – where “awaiting” the result of a call to (e.g.) httpClient.GetAsync(…) will never return. This … Read more