Search code examples
c#typesrequestresponse

C# Get content type of a response without loading entire content


I want to make sure from those who know if the following lines of code will only get the response type of the request without loading the full content, because i have to to some conditions over response type in my application and i dont want to waste time for that resources that should be ignored.

request = (HttpWebRequest)System.Net.HttpWebRequest.Create(url);
response = (HttpWebResponse)request.GetResponse();
var responseType = response.ContentType;

Solution

  • It's probably easiest to set the Method property to "HEAD" - that way you won't get the actual contents, just the headers.

    On the other hand, that does mean you'll need to make two requests when you do want the contents...