Submitted by: Srinath M S(

Tuesday Jan 23rd 2018 by Srinath M S

Language: .NET, Level: Intermediate: - See how to use the HttpWebRequest class in C# to determine the size of a file.

There are cases in which we might want to warn the user before an attempt is made to download a large file, or we might simply be interested to know its size before downloading.

In C#, we use the HttpWebRequest class to download objects from an URL. We can set the request method to be of "Head" type and examine the response header "Content-Length" to determine the size of the object.

string url = "";
var urlRequest = (HttpWebRequest)WebRequest.Create(url);
//Set the request.method to ???HEAD???
urlRequest.Method = "HEAD";

var urlResponse = urlRequest.GetResponse();
string contentLength = urlResponse.Headers.Get("Content-Length");
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved