[Serializable] |
You should never directly create an instance of the HttpWebResponse class. Instead, use the instance returned by a call to HttpWebRequest.GetResponse.
Common header information returned from the Internet resource is exposed as properties of the class. See the following table for a complete list. Other headers can be read from the HttpWebResponse.Headers property as name/value pairs.
The following table shows the common HTTP headers that are available through properties of the HttpWebResponse class.
Header | Property |
---|---|
Content-Encoding | HttpWebResponse.ContentEncoding |
Content-Length | HttpWebResponse.ContentLength |
Content-Type | HttpWebResponse.ContentType |
Last-Modified | HttpWebResponse.LastModified |
Server | HttpWebResponse.Server |
The contents of the response from the Internet resource are returned as a Stream by calling the HttpWebResponse.GetResponseStream method.
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.contoso.com"); HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); // Insert code that uses the response object. HttpWResp.Close();
CharacterSet | Read-only Gets the character set of the response. |
ContentEncoding | Read-only Gets the method used to encode the body of the response. |
ContentLength | Read-only Overridden: Gets the length of the content returned by the request. |
ContentType | Read-only Overridden: Gets the content type of the response. |
Cookies | Read-write Gets or sets the cookies associated with this request. |
Headers | Read-only Overridden: Gets the headers associated with this response from the server. |
LastModified | Read-only Gets the last date and time that the contents of the response were modified. |
Method | Read-only Gets the method used to return the response. |
ProtocolVersion | Read-only Gets the version of the HTTP protocol used in the response. |
ResponseUri | Read-only Overridden: Gets the URI of the Internet resource that responded to the request. |
Server | Read-only Gets the name of the server that sent the response. |
StatusCode | Read-only Gets the status of the response. |
StatusDescription | Read-only Gets the status description returned with the response. |
Close | Overridden: Closes the response stream. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode | Overridden: |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetResponseHeader | Gets a specified header contents that was returned with the response. |
GetResponseStream | Overridden: Gets the stream used to read the body of the response from the server. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
ctor #1 | Initializes a new instance of the HttpWebResponse class from the specified SerializationInfo and StreamingContext instances. |
Dispose | |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
protected HttpWebResponse( |
serializationInfo
streamingContext
public string CharacterSet {get;}
|
public string ContentEncoding {get;}
|
public override long ContentLength {get;}
|
Stream receiveStream = myHttpWebResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader readStream = new StreamReader( receiveStream, encode ); Console.WriteLine("\r\nResponse stream received."); char[] read = new char[256]; int count = readStream.Read( read, 0, 256 ); Console.WriteLine("\nText retrieved from the URL follows:\r\n"); int index = 0; while (index < myHttpWebResponse.ContentLength) { // Dumps the 256 characters on a string and displays the string to the console. String str = new String(read, 0, count); Console.WriteLine(str); index += count; count = readStream.Read(read, 0, 256); } // Releases the resources of the Stream. receiveStream.Close(); Console.WriteLine(""); } // Releases the resources of the response. myHttpWebResponse.Close();
public override string ContentType {get;}
|
try { HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); char seperator = '/'; String contenttype = myHttpWebResponse.ContentType; // Retrieves the text if the content type is of text/html. String maintype = contenttype.Substring(0,contenttype.IndexOf(seperator)); // Displays only text type. if (String.Compare(maintype,"text") == 0) { Console.WriteLine("\n Content type is text."); . . . Stream receiveStream = myHttpWebResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader readStream = new StreamReader( receiveStream, encode ); Console.WriteLine("\r\nResponse stream received."); char[] read = new char[256]; int count = readStream.Read( read, 0, 256 ); Console.WriteLine("\nText retrieved from the URL follows:\r\n"); int index = 0; while (index < myHttpWebResponse.ContentLength) { // Dumps the 256 characters on a string and displays the string to the console. String str = new String(read, 0, count); Console.WriteLine(str); index += count; count = readStream.Read(read, 0, 256); } // Releases the resources of the Stream. receiveStream.Close(); Console.WriteLine(""); } // Releases the resources of the response. myHttpWebResponse.Close();
public CookieCollection Cookies {get; set;}
|
If the HttpWebRequest.CookieContainer property of the associated HttpWebRequest is null, the HttpWebResponse.Cookies property will also be null. Any cookie information sent by the server will be available in the HttpWebResponse.Headers property, however.
try { HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); CookieCollection myCookieCollection = myHttpWebResponse.Cookies; for (int i = 0; i < myCookieCollection.Count; i++){ Console.WriteLine(myCookieCollection[i]); } myHttpWebResponse.Close(); } catch(WebException e) { Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status); }
public override WebHeaderCollection Headers {get;}
|
Header | Property |
---|---|
Content-Encoding | HttpWebResponse.ContentEncoding |
Content-Length | HttpWebResponse.ContentLength |
Content-Type | HttpWebResponse.ContentType |
Last-Modified | HttpWebResponse.LastModified |
Server | HttpWebResponse.Server |
// Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // Sends the HttpWebRequest and waits for response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); // Displays all the headers present in the response received from the URI. Console.WriteLine("\r\nThe following headers were received in the response:"); // Displays each header and it's key associated with the response. for(int i=0; i < myHttpWebResponse.Headers.Count; ++i) Console.WriteLine("\nHeader Name:{0}, Value :{1}",myHttpWebResponse.Headers.Keys[i],myHttpWebResponse.Headers[i]); // Releases the resources of the response. myHttpWebResponse.Close();
public DateTime LastModified {get;}
|
Uri myUri = new Uri(url); // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); if (myHttpWebResponse.StatusCode == HttpStatusCode.OK) Console.WriteLine("\r\nRequest succeeded and the requested information is in the response , Description : {0}", myHttpWebResponse.StatusDescription); DateTime today = DateTime.Now; // Uses the LastModified property to compare with today's date. if (DateTime.Compare(today,myHttpWebResponse.LastModified) == 0) Console.WriteLine("\nThe requested URI entity was modified today"); else if (DateTime.Compare(today,myHttpWebResponse.LastModified) == 1) Console.WriteLine("\nThe requested URI was last modified on:{0}", myHttpWebResponse.LastModified); // Releases the resources of the response. myHttpWebResponse.Close();
public string Method {get;}
|
try { // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); string method ; method = myHttpWebResponse.Method; if (String.Compare(method,"GET") == 0) Console.WriteLine("\nThe 'GET' method was successfully invoked on the following Web Server : {0}", myHttpWebResponse.Server); // Releases the resources of the response. myHttpWebResponse.Close(); } catch(WebException e) { Console.WriteLine("\nWebException raised. The following error occured : {0}",e.Status); } catch(Exception e) { Console.WriteLine("\nThe following Exception was raised : {0}",e.Message); } }
public Version ProtocolVersion {get;}
|
Uri ourUri = new Uri(url); // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri); myHttpWebRequest.ProtocolVersion = HttpVersion.Version10; // Sends the HttpWebRequest and waits for the response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); // Ensures that only Http/1.0 responses are accepted. if(myHttpWebResponse.ProtocolVersion != HttpVersion.Version10) Console.WriteLine("\nThe server responded with a version other than Http/1.0"); else if (myHttpWebResponse.StatusCode == HttpStatusCode.OK) Console.WriteLine("\nRequest sent using version Http/1.0. Successfully received response with version HTTP/1.0 "); // Releases the resources of the response. myHttpWebResponse.Close();
public override Uri ResponseUri {get;}
|
public string Server {get;}
|
try { // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); string method ; method = myHttpWebResponse.Method; if (String.Compare(method,"GET") == 0) Console.WriteLine("\nThe 'GET' method was successfully invoked on the following Web Server : {0}", myHttpWebResponse.Server); // Releases the resources of the response. myHttpWebResponse.Close(); } catch(WebException e) { Console.WriteLine("\nWebException raised. The following error occured : {0}",e.Status); } catch(Exception e) { Console.WriteLine("\nThe following Exception was raised : {0}",e.Message); } }
public HttpStatusCode StatusCode {get;}
|
public static void GetPage(String url) { try { // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // Sends the HttpWebRequest and waits for a response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); if (myHttpWebResponse.StatusCode == HttpStatusCode.OK) Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}", myHttpWebResponse.StatusDescription); // Releases the resources of the response. myHttpWebResponse.Close(); } catch(WebException e) { Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status); } catch(Exception e) { Console.WriteLine("\nThe following Exception was raised : {0}",e.Message); } }
public string StatusDescription {get;}
|
public static void GetPage(String url) { try { // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // Sends the HttpWebRequest and waits for a response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); if (myHttpWebResponse.StatusCode == HttpStatusCode.OK) Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}", myHttpWebResponse.StatusDescription); // Releases the resources of the response. myHttpWebResponse.Close(); } catch(WebException e) { Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status); } catch(Exception e) { Console.WriteLine("\nThe following Exception was raised : {0}",e.Message); } }
public override void Close(); |
// Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // Sends the HttpWebRequest and waits for a response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Console.WriteLine("\nResponse Received.Trying to Close the response stream.."); // Releases the resources of the response. myHttpWebResponse.Close(); Console.WriteLine("\nResponse Stream successfully closed");
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
protected virtual void Dispose( |
disposing
~HttpWebResponse(); |
public override int GetHashCode(); |
public object GetLifetimeService(); |
headerName
public static void GetPage(String url) { try { Uri ourUri = new Uri(url); // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Console.WriteLine("\nThe server did not issue any challenge. Please try again with a protected resource URL."); // Releases the resources of the response. myHttpWebResponse.Close(); } catch(WebException e) { HttpWebResponse response = (HttpWebResponse)e.Response; if (response != null) { if (response.StatusCode == HttpStatusCode.Unauthorized) { string challenge = null; challenge= response.GetResponseHeader("WWW-Authenticate"); if (challenge != null) Console.WriteLine("\nThe following challenge was raised by the server:{0}",challenge); } else Console.WriteLine("\nThe following WebException was raised : {0}",e.Message); } else Console.WriteLine("\nResponse Received from server was null"); } catch(Exception e) { Console.WriteLine("\nThe following Exception was raised : {0}",e.Message); } } }
public override Stream GetResponseStream(); |
Exception Type | Condition |
---|---|
ProtocolViolationException | There is no response stream. |
// Creates an HttpWebRequest with the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); // Sends the HttpWebRequest and waits for the response. HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); // Gets the stream associated with the response. Stream receiveStream = myHttpWebResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); // Pipes the stream to a higher level stream reader with the required encoding format. StreamReader readStream = new StreamReader( receiveStream, encode ); Console.WriteLine("\r\nResponse stream received."); Char[] read = new Char[256]; // Reads 256 characters at a time. int count = readStream.Read( read, 0, 256 ); Console.WriteLine("HTML...\r\n"); while (count > 0) { // Dumps the 256 characters on a string and displays the string to the console. String str = new String(read, 0, count); Console.Write(str); count = readStream.Read(read, 0, 256); } Console.WriteLine(""); // Releases the resources of the response. myHttpWebResponse.Close(); // Releases the resources of the Stream. readStream.Close();
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
public virtual string ToString(); |