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!
Asked
Active
Viewed 516 times
-1
-
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 Answers
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
-
And then he can get the acutal "size" by getting the byte count of the requested site... System.Text.ASCIIEncoding.Unicode.GetByteCount(s); – oopbase Jan 13 '17 at 13:12
-
1@oopbase - thanks for the constructive comment. I had missed the purpose of the question :). – Alexei - check Codidact Jan 13 '17 at 13:15
-
Please at least use `System.Text.Encoding.Unicode`. `System.Text.ASCIIEncoding.Unicode` works but looks wrong and doesn't make any sense. – Manfred Radlwimmer Jan 13 '17 at 13:17
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.

Matteo Marciano - MSCP
- 419
- 2
- 5