ac-lap
ac-lap

Reputation: 1653

Getting correct download file size from url

I am trying to get the file size from url (https://windows.php.net/downloads/releases/php-7.2.9-nts-Win32-VC15-x64.zip) and here is the code I have -

HttpWebRequest request = HttpWebRequest.CreateHttp(url);
HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync());
long length = response.ContentLength;

But the value of length is 598 bytes whereas the website (and when downloading from browser) reports the size as 24.5MB. I even tried accessing the "Content-Length" from the response header, but it also had the same value, 598.

Am I missing something? Is there any other way to get the file sizes more accurately?

Upvotes: 3

Views: 7930

Answers (1)

Subbu
Subbu

Reputation: 2205

I used your example URL and read the contents via:

var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
var responseFromServer = reader.ReadToEnd();

The result I get is:

20/feb/2018: Hi! We seem to be receiving high volume requests coming from empty user agents. While this shouldn't be an issue, this unfortunately resulted in bandwidth issues on this server causing all downloads to be unavailable. We've therefore decided to temporarily block empty user agents until we upgraded our server bandwidth.

03/mar/2018: We've upgraded the server bandwidth. This is however still not sufficient to handle all empty user agent connections. Please update the user agent in your scripts accordingly or contact us so we can discuss it.

Thank you for your understanding.

It says that set UserAgent. So, I set the user agent as follows:

var request = HttpWebRequest.CreateHttp(url);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
var response = (HttpWebResponse)(await request.GetResponseAsync());
var length = response.ContentLength;

Now, I get the correct Content-Length value of 25691309

I just picked an user agent string from: http://www.useragentstring.com/index.php?id=19879

If you just interested in the size of the remote file, you should consider the answer to the linked question. It essentially uses a different HTTP Metho (HEAD vs GET)

        var request = HttpWebRequest.CreateHttp(url);
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
        request.Method = "HEAD";
        using (var response = await request.GetResponseAsync())
        {
            var length = response.ContentLength;
        }

You are find more details about HEAD Vs GET in the related question: http HEAD vs GET performance

Upvotes: 5

Related Questions