HTTP response using the GetResponseHeader () class HttpWebResponse

The ContentLength property of the HttpWebResponse class can be used when we want to get the size of an HTTP response, the number of bytes contained in the response. This can be useful when we are using the HttpWebRequest and HttpWebResponse to download a file using the HTTP protocol. In this case it is interesting to know the size of the file being downloaded to show the user the progress of the operation (with download progress bar).

The method GetResponseHeader () as HttpWebResponse class can be used when we want to get the values of an HTTP response headers. See your signature:

public string HttpWebResponse.GetResponseHeader(
string headerName

Note that this method takes a string indicating the name of the header whose content we want to access and returns a string representing the content. See, for example, how can we access the contents of the header Content-Length:

static void Main (string [] args) {
/ / we set the URL to be accessed
string url = ""; // or any type of file...

/ / let's create a HttpWebRequest object
HttpWebRequest httpRequest = (HttpWebRequest) WebRequest.Create (url);

/ / we get an instance of HttpWebResponse
HttpWebResponse = HttpWebResponse (HttpWebResponse) httpRequest.GetResponse ();

/ / we get the value of Content-Length header
string value = httpWebResponse.GetResponseHeader ("Content-Length");

/ / will display the result
Console.WriteLine ("The return value in the header is Content-Length: " +

Console.Write ("\ n \ nPress a key to exit ...");
Console.ReadKey ();

Some names of headers that you can use are: Last-Modified, Content-Range, Content-Length, Content-Type, Expires, If-Modified-Since, etc..

Thanks all

Recommended Ebook

ASP.NET GridView Control Pocket Guide

Download Now!
Similar Articles