-1

I want to compute the size in KB or Bytes of my website's html. I know that there are several websites that can provide me this value, but I don't want to use them. I'd like to use my own code. Could you please help me? Thank you!

  • Most websites contain dynamic content so the size will not be fixed. If you want to minimize traffic use a [http HEAD](http://stackoverflow.com/questions/3268926/head-with-webclient) request instead. The size (if provided) will be in the header field `Content-Length`. – Manfred Radlwimmer Jan 13 '17 at 13:14

2 Answers2

1

If you want to see the size of the HTML loaded initially by your browser, you can use WebClient like below:

using (var client = new WebClient()) 
{
   string s = client.DownloadString(url);
   // s contains the whole document text
   int strCount = s.Length;
   int byteCount = System.Text.Encoding.Unicode.GetByteCount(s);
}

However, keep in mind that the actually downloaded data may increase due to AJAX calls and dynamically created DOM elements (after document is ready).

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
1

An efficient way to do this would be to use WebRequest class and read the ContentLength property without actually reading the content, to avoid unnecessary data download.

Also, as stated by @Manfred Radlwimmer, using HTTP HEAD would be a more efficient way.