'Asp.Net Core http requests shows Headers.ContentLength

I am doing an http get call and I can see that Headers.ContentLength always returns null whereas I can see that in fiddler a value: This is my code

string url = $"url";

HttpClientHandler httpClientHandler = new HttpClientHandler();

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;

HttpClient httpClient = new HttpClient(httpClientHandler)
{
    BaseAddress = new Uri(url)
};

httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {authToken.Value}");
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");

httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

HttpResponseMessage response = await httpClient.GetAsync(string.Empty, HttpCompletionOption.ResponseHeadersRead);

response.EnsureSuccessStatusCode();


var responseString = await response.Content.ReadAsStringAsync();

var length = response.Content.Headers.ContentLength;

ContentLength is always null. Fiddler below shows body length and also header length and Im downloading the data without any issue.

enter image description here

enter image description here

I need to be able to read content length header to decide if I will allow download. Why am I receiving null



Solution 1:[1]

Set a breakpoint in your HttpResponseMessage response to see what the status of the response looks like, and check which step is wrong, because your code as a whole is fine.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Qing Guo