SIGN UP MEMBER LOGIN:    
ARTICLE

HTTP response using the GetResponseHeader () class HttpWebResponse

Posted by Alan Le Senechal Articles | ASP.NET Programming June 05, 2011
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.
Reader Level:

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 = "http://www.xxx.com/file.pdf"; // 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: " +
value);

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
 

Login to add your contents and source code to this article
share this article :
post comment
 

Good Work, Welcome to the Mindcracker Community.

Posted by Dinesh Beniwal Jun 05, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor