C# Tip Article
How to check the size of HTTP resource
Sometimes we want to know the size of HTTP resource before actually getting it. For example, we want to know video file size before downloading it from a web site. HTTP HEAD command is very useful for this purpose. With HTTP GET command, server actually sends data to client, but when using HEAD command, server does not send data except header metadata.
The following sample shows how to get the size of image file with HEAD command.
string url = "http://www.csharpstudy.com/image/csharpstudy160.png"; var req = (HttpWebRequest)WebRequest.Create(url); req.Method = "HEAD"; var resp = req.GetResponse(); string strLength = resp.Headers.Get("Content-Length"); resp.Close(); int size = int.Parse(strLength); Console.WriteLine(size);