Accessing the Header


You can access the header information associated with an HTTP response through the Headers property defined by HttpWebResponse. It is shown here:

 public WebHeaderCollection Headers{ get; }

An HTTP header consists of pairs of names and values represented as strings. Each name/value pair is stored in a WebHeaderCollection. This specialized collection stores key/value pairs and can be used like any other collection. (See Chapter 23.) A string array of the names can be obtained from the AllKeys property. You can obtain the value associated with a name by using the indexer. The indexer is overloaded to accept a numeric index, the name, or a value from either the HttpRequestHeader or HttpResponseHeader enumerations.

The following program displays all of the headers associated with McGraw-Hill.com:

 // Examine the headers. using System; using System.Net; class HeaderDemo {   public static void Main() {     // Create a WebRequest to a URI.     HttpWebRequest req = (HttpWebRequest)            WebRequest.Create("http://www.McGraw-Hill.com");     // Send that request and return the response.     HttpWebResponse resp = (HttpWebResponse)            req.GetResponse();     // Obtain a list of the names.     string[] names = resp.Headers.AllKeys;     // Display the header name/value pairs.     Console.WriteLine("{0,-20}{1}\n", "Name", "Value");     foreach(string n in names)       Console.WriteLine("{0,-20}{1}", n, resp.Headers[n]);     // Close the Response.     resp.Close();   } }

Here is the output that was produced. (Remember, all header information is subject to change, so the precise output that you see may differ.)

 Name                Value Transfer-Encoding   chunked Content-Type        text/html Date                Tue, 28 Jun 2005 19:59:26 GMT Server              Netscape-Enterprise/6.0

Accessing Cookies

You can gain access to the cookies associated with an HTTP response through the Cookies property defined by HttpWebResponse. Cookies contain information that is stored by a browser. They consist of name/value pairs, and they facilitate certain types of Web access. Cookies is defined like this:

 public CookieCollection Cookies { get; set; }

CookieCollection implements ICollection and IEnumerable, and can be used like any other collection. (See Chapter 23.) It has an indexer that allows a cookie to be obtained by specifying its index or its name.

CookieCollection stores objects of type Cookie. Cookie defines several properties that give you access to the various pieces of information associated with a cookie. The two that we will use here are Name and Value, which are defined like this:

 public string Name { get; set; } public string Value { get; set; }

The name of the cookie is contained in Name, and its value is found in Value.

To obtain a list of the cookies associated with a response, you must supply a cookie container in the request. For this purpose, HttpWebRequest defines the property CookieContainer, shown here:

 public CookieContainer CookieContainer { get; set; }

CookieContainer provides various fields, properties, and methods that let you store cookies. However, for many applications, you won’t need to work with CookieContainer directly. Instead, you will use the CookieCollection obtained from the response. The CookieContainer simply provides the underlying storage mechanism for the cookies.

The following program displays the names and values of the cookies associated with the URI specified on the command line. Remember, not all web sites use cookies, so you might have to try a few until you find one that does.

 /* Examine Cookies.    To see what cookies a web site uses,    specify its name on the command line.    For example, if you call this program    CookieDemo, then      CookieDemo http://msn.com    displays the cookies associated with msn.com. */ using System; using System.Net; class CookieDemo {   public static void Main(string[] args) {     if(args.Length != 1) {       Console.WriteLine("Usage: CookieDemo <uri>");       return ;     }     // Create a WebRequest to the specified URI.     HttpWebRequest req = (HttpWebRequest)            WebRequest.Create(args[0]);     // Get an empty cookie container.     req.CookieContainer = new CookieContainer();          // Send the request and return the response.     HttpWebResponse resp = (HttpWebResponse)            req.GetResponse();     // Display the cookies.     Console.WriteLine("Number of cookies: " +                         resp.Cookies.Count);     Console.WriteLine("{0,-20}{1}", "Name", "Value");     for(int i=0; i < resp.Cookies.Count; i++)       Console.WriteLine("{0, -20}{1}",                          resp.Cookies[i].Name,                          resp.Cookies[i].Value);     // Close the Response.     resp.Close();   } }

Using the LastModified Property

Sometimes you will want to know when a resource was last updated. This is easy to find out when using HttpWebResponse, because it defines the LastModified property. It is shown here:

 public DateTime LastModifi ed { get; }

LastModified obtains the time that the content of the resource was last modified.

The following program displays the time and date at which the URI entered on the command-line was last updated:

 /* Use LastModified.    To see the date on which a web site was    last modified, enter its URI on the command    line. For example, if you call this program    LastModifiedDemo, then to see the date of last    modification for HerbSchildt.com enter      LastModifiedDemo http://HerbSchildt.com */ using System; using System.Net; class LastModifiedDemo {   public static void Main(string[] args) {     if(args.Length != 1) {       Console.WriteLine("Usage: LastModifiedDemo <uri>");       return ;     }     HttpWebRequest req = (HttpWebRequest)            WebRequest.Create(args[0]);     HttpWebResponse resp = (HttpWebResponse)            req.GetResponse();     Console.WriteLine("Last modified: " + resp.LastModified);     resp.Close();   } }




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net