Questions tagged [dotnet-httpclient]

Refers to the System.Net.Http.HttpClient library which is part of the HttpClient package on NuGet, or the .NET 4.5 library.

System.Net.Http.HttpClient is a general purpose HTTP client library, intended for use in REST applications or basic HTTP applications. It includes support for de-serializing responses in JSON and XML.

HttpClient was first released in 2008/9 as part of the WCF Starter Kit, which was released via Codeplex.

That HTTP Client library then graduated to a standalone package available via NuGet.

HttpClient will be subsumed into .NET with v4.5, retaining the fullname, namespace, and API, but changing the strong name.

This tag refers to all versions of that .NET library; It specifically excludes the similarly-named Java HttpClient library.

See also for proper HttpClient lifecycle management and creating typed clients.

2837 questions
1096
votes
24 answers

How to set the Content-Type header for an HttpClient request?

I'm trying to set the Content-Type header of an HttpClient object as required by an API I am calling. I tried setting the Content-Type like below: using (var httpClient = new HttpClient()) { httpClient.BaseAddress = new…
mynameiscoffey
  • 15,244
  • 5
  • 33
  • 45
521
votes
5 answers

Why is HttpClient BaseAddress not working?

Consider the following code, where the BaseAddress defines a partial URI path. using (var handler = new HttpClientHandler()) using (var client = new HttpClient(handler)) { client.BaseAddress = new Uri("http://something.com/api"); var…
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
412
votes
12 answers

Do HttpClient and HttpClientHandler have to be disposed between requests?

System.Net.Http.HttpClient and System.Net.Http.HttpClientHandler in .NET Framework 4.5 implement IDisposable (via System.Net.Http.HttpMessageInvoker). The using statement documentation says: As a rule, when you use an IDisposable object, you should…
Fernando Correia
  • 21,803
  • 13
  • 83
  • 116
371
votes
7 answers

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…
Benjamin Fox
  • 5,624
  • 5
  • 19
  • 19
355
votes
3 answers

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

public static async Task GetData(string url, string data) { UriBuilder fullUri = new UriBuilder(url); if (!string.IsNullOrEmpty(data)) fullUri.Query = data; HttpClient client = new HttpClient(); HttpResponseMessage…
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
318
votes
15 answers

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…
Jidheesh Rajan
  • 4,744
  • 4
  • 23
  • 29
271
votes
6 answers

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…
Thought
  • 5,326
  • 7
  • 33
  • 69
271
votes
6 answers

Deciding between HttpClient and WebClient

Our web application is running in .NET Framework 4.0. The UI calls the controller methods through Ajax calls. We need to consume the REST service from our vendor. I am evaluating the best way to call the REST service in .NET 4.0. The REST service…
user3092913
  • 2,735
  • 2
  • 12
  • 4
256
votes
9 answers

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 allTasks = new List(); allTasks.Add(....); allTasks.Add(....); Task.WaitAll(allTasks.ToArray(),…
One Developer
  • 99
  • 5
  • 43
  • 103
232
votes
7 answers

Why use HttpClient for Synchronous Connection

I am building a class library to interact with an API. I need to call the API and process the XML response. I can see the benefits of using HttpClient for Asynchronous connectivity, but what I am doing is purely synchronous, so I cannot see any…
Ketchup
  • 3,071
  • 4
  • 20
  • 23
225
votes
9 answers

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()) { …
Libin Joseph
  • 7,070
  • 5
  • 29
  • 52
212
votes
5 answers

.NET HttpClient. How to POST string value?

How can I create using C# and HttpClient the following POST request: I need such a request for my WEB API service: [ActionName("exist")] [HttpPost] public bool CheckIfUserExist([FromBody] string login) { return…
Ievgen Martynov
  • 7,870
  • 8
  • 36
  • 52
207
votes
2 answers

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…
Ryan Pfister
  • 3,176
  • 4
  • 24
  • 26
203
votes
3 answers

Is HttpClient safe to use concurrently?

In all the examples I can find of usages of HttpClient, it is used for one off calls. But what if I have a persistent client situation, where several requests can be made concurrently? Basically, is it safe to call client.PostAsync on 2 threads at…
Alex K
  • 10,835
  • 8
  • 29
  • 34
201
votes
6 answers

How can I tell when HttpClient has timed out?

As far as I can tell, there's no way to know that it's specifically a timeout that has occurred. Am I not looking in the right place, or am I missing something bigger? string baseAddress = "http://localhost:8080/"; var client = new HttpClient() { …
Benjol
  • 63,995
  • 54
  • 186
  • 268
1
2 3
99 100