C# – How to get the status code when using HttpClient

When you use HttpClient to make requests, you can directly get the status code from the HttpResponseMessage object, like this: The main reason for checking the status code is to determine if the request was successful and then reacting to error status codes (usually by throwing an exception). The HttpResponseMessage class has two helpers that … Read more

C# – Handling redirects with HttpClient

HttpClient handles redirects automatically. When you send a request, if the response contains a redirect status code (3xx) and redirect location, then it’ll send a new request to the redirect location. You can turn off this auto-redirect behavior by passing in an HttpClientHandler with AllowAutoRedirect=false. This prevents it from following redirects automatically, and allows you … Read more

C# – Configuring how long an HttpClient connection will stay open

When you use a single instance of HttpClient to send requests, it keeps connections open in order to speed up future requests. By default, idle connections are closed after 2 minutes, and otherwise will be kept open forever (in theory). In reality, the connection can be closed by the server-side (or other external factors) regardless … Read more

C# – The performance gains of HttpClient reusing connections

When you use the same instance of HttpClient for multiple requests (sequential and concurrent) to the same URL, it’ll reuse connections. Requests that get to reuse a connection are 5.5-8.5x faster than requests that have to open a new connection. There are a few scenarios that benefit from this connection reuse: Sending sequential requests frequently. … Read more

C# – How to read response headers with HttpClient

When you send a request with HttpClient, it returns an HttpResponseMessage. You can read the response headers through the HttpResponseMessage.Headers property: This outputs the response headers: Raw response headers are really just key/value(s) pairs. When the response comes in, the headers are loaded into the Headers property (which is of type HttpResponseHeaders). This parses the … Read more

C# – How to cancel an HttpClient request

It’s a good idea to provide users with a way to cancel a HttpClient request that’s taking too long. To be able to cancel an HttpClient request, you can pass in a CancellationToken: To get a CancellationToken, you have to create a CancellationTokenSource: To actually cancel the request, you have to call CancellationTokenSource.Cancel(): This means … Read more

C# – Sending query strings with HttpClient

Query strings start with ‘?’ and have one or more key-value pairs separated by ‘&’. All characters except a-z, A-Z, 0-9 have to be encoded, including Unicode characters. When you use HttpClient, it automatically encodes the URI for you (internally, it delegates this task to the Uri class). This means when you include a query … Read more

C# – How to add request headers when using HttpClient

There are two ways add request headers when using HttpClient: Add headers for all requests using HttpClient.DefaultRequestHeaders. Add headers per request using HttpRequestMessage.Headers. In this article, I’ll show examples of both ways to add request headers. Add an unchanging header for all requests Let’s say you’re adding an API Key header. It needs to be … Read more

C# – How to use Polly to do retries

Whenever you’re dealing with code that can run into transient errors, it’s a good idea to implement retries. Transient errors, by definition, are temporary and subsequent attempts should succeed. When you retry with a delay, it means you think the the transient error will go away by itself after a short period of time. When … Read more

C# – How to change the HttpClient timeout per request

It’s best practice to reuse the same HttpClient instance for multiple requests. When you’re using the same instance repeatedly, and you want to change the timeout per request, you can pass in a CancellationToken, like this: You can’t change HttpClient.Timeout after the instance has been used. You have to pass in a CancellationToken instead. There … Read more