I'm attempting to get the content-length of a remote file so that I can limit the download size of remote images thus:
WebResponse response = this.GetWebRequest().GetResponse();
long contentLength = response.ContentLength;
// WebResponse.ContentLength doesn't always know the value,
// it returns -1 in this case.
if (contentLength == -1)
{
// Response headers may still have the Content-Length inside of it.
string headerContentLength = response.Headers["Content-Length"];
if (!String.IsNullOrWhiteSpace(headerContentLength))
{
contentLength = long.Parse(headerContentLength,
CultureInfo.InvariantCulture);
}
}
My problem is that since the content-length header is optional, sometimes that value is indeterminable.
I have read in a similar questions here and here that seems to indicate that I could somehow use the Transfer-Encoding=chunked property to determine the length but I have found no example of how to do so.
Is this possible? Can anyone provide me with a code example?