Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 394
Alex Avatar asked Feb 03 '26 09:02

Alex


1 Answers

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...

like image 180
Jon Skeet Avatar answered Feb 05 '26 21:02

Jon Skeet