I need to detect a type of content located on specific URL. So I created a method to get the Content-Type of response. For small files and HTML pages it works without problems, but if URL points to a big file, request takes a long time - it fetches entire content (file) on background. So, it is possible to cancel request and return result immediately after teh Content-Type
header is received?
My current implementation:
public async static Task<string> GetContentType(string url)
{
try
{
using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
return null;
}
return response.Content.Headers.ContentType.MediaType;
}
}
catch (HttpRequestException)
{
return null;
}
}