PART II
C h a p t e r 2 5 :
N e t w o r k i n g T h r o u g h t h e I n t e r n e t U s i n g S y s t e m . N e t
835
The following program displays headers associated with McGraw-Hill.com:
// Examine the headers.
using System;
using System.Net;
Property
Description
public string CharacterSet { get; }
Obtains the name of the character set being used.
public string ContentEncoding { get; }
Obtains the name of the encoding scheme.
public long ContentLength { get; }
Obtains the length of the content being received. This
will be –1 if the content length is not available.
public string ContentType { get; }
Obtains a description of the content.
public CookieCollection
Cookies { get; set; }
Obtains or sets a list of the cookies attached to the
response.
public WebHeaderCollection
Headers{ get; }
Obtains a collection of the headers attached to the
response.
public bool IsFromCache { get; }
If the response came from the cache, this proper ty is
true. It is false if the response was delivered over the
network.
public bool
IsMutuallyAuthenticated { get; }
If the client and ser ver are both authenticated, then
this proper ty is true. It is false other wise.
public DateTime LastModified { get; }
Obtains the time at which the resource was last
changed.
public string Method { get; }
Obtains a string that specifies the response method.
public Version ProtocolVersion { get; }
Obtains a
Version
object that describes the version of
HTTP used in the transaction.
public Uri ReponseUri { get; }
Obtains the URI that generated the response. This
may differ from the one requested if the response
was redirected to another URI.
public string Ser ver { get; }
Obtains a string that represents the name of the
ser ver.
public HttpStatusCode StatusCode { get; }
Obtains an
HttpStatusCode
object that describes the
status of the transaction.
public string StatusDescription { get; }
Obtains a string that represents the status of the
transaction in a human-readable form.
T
ABLE
25-5
The Proper ties Defi ned by
HttpWebResponse
www.freepdf-books.com
836
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
class HeaderDemo {
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.Write("{0,-20}", n);
foreach(string v in resp.Headers.GetValues(n))
Console.WriteLine(v);
}
// 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 Fri, 27 Jun 2008 20:32:06 GMT
Server Sun-ONE-Web-Server/6.1
Do'stlaringiz bilan baham: |