Reading content length of website

I am trying to get the content length of the web page. example http://www.google.com.

I am using c# and below is the code I used and does not give me correct length or does it. Can some one validate please.

var request = (HttpWebRequest)WebRequest.Create("http://www.google.com.au");
request.Method = "GET";
var myResponse = request.GetResponse();
var responseLength = myResponse.ContentLength;

using (var sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8))
{
    var result = sr.ReadToEnd();
    myResponse.Close();
}

responseLength is -1 allways but result.Length has some value, is that correct?

Jon Skeet
people
quotationmark

responseLength is -1 allways but result.Length has some value, is that correct?

Well it may be for some web sites (or some responses in some web sites) - in other cases, you'll see a non-negative value for responseLength. All you're doing is fetching the optional Content-Length HTTP header, basically... it's up to the server whether it supplies that or not.

Note that the response length, if provided, will be in bytes - whereas result.Length is in UTF-16 code units. If you want the content length in bytes, you should be reading the binary data from the stream directly rather than creating a StreamReader and reading it as text.

people

See more on this question at Stackoverflow