Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP HEAD request with HttpClient in .NET 4.5 and C#

Is it possible to create a HTTP HEAD request with the new HttpClient in .NET 4.5? The only methods I can find are GetAsync, DeleteAsync, PutAsync and PostAsync. I know that the HttpWebRequest-class is able to do that, but I want to use the modern HttpClient.

like image 380
The Wavelength Avatar asked May 07 '13 10:05

The Wavelength


People also ask

How do I make a HTTP HEAD request?

To make a HEAD request with Curl, you need to use the -I or --head command-line parameter. The -I command-line parameter tells Curl to send an HTTP HEAD request to receive only HTTP headers. The HEAD request is very similar to a GET request, except that the server only returns HTTP headers without a response body.

What is the use of HttpClient in C#?

HttpClient class provides a base class for sending/receiving the HTTP requests/responses from a URL. It is a supported async feature of . NET framework. HttpClient is able to process multiple concurrent requests.

What is HTTP HEAD request?

HEAD Http Method HEAD is a request method supported by HTTP used by the World Wide Web. The HEAD method asks for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.


1 Answers

Use the SendAsync method with an instance of HttpRequestMessage that was constructed using HttpMethod.Head .

GetAsync, PostAsync, etc are convenient wrappers around SendAsync; the less common HTTP methods such as HEAD, OPTIONS, etc, don't get a wrapper.

In short:

client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url))

like image 72
Smigs Avatar answered Sep 28 '22 05:09

Smigs