System.Net.WebResponse Class

Assembly: System.dll
Namespace: System.Net
Summary
Provides a response from a Uniform Resource Identifier (URI). This is an abstract class.
C# Syntax:
[Serializable]
public abstract class WebResponse : MarshalByRefObject, ISerializable, IDisposable
Remarks
The WebResponse class is the abstract base class from which protocol-specific response classes are derived. Applications can participate in request and response transactions in a protocol-agnostic manner using instances of the WebResponse class while protocol-specific classes derived from WebResponse carry out the details of the request.

Client applications do not create WebResponse objects directly, they are created by calling the WebRequest.GetResponse method on a WebRequest instance.



Notes to inheritors: When you inherit from WebResponse, you must override the following members: WebResponse.ContentLength, WebResponse.ContentType, WebResponse.GetResponseStream, WebResponse.ResponseUri, and WebResponse.Headers.
Example
The following example creates a WebResponse instance from a WebRequest.
// Initialize the WebRequest.
WebRequest myRequest = WebRequest.Create("http://www.contoso.com");

// Return the response. 
WebResponse myResponse = myRequest.GetResponse();

// Code to use the WebResponse goes here.

// Close the response to free resources.
myResponse.Close();


    
See also:
System.Net Namespace

System.Net.WebResponse Member List:

Public Properties
ContentLength Read-write

When overridden in a descendant class, gets or sets the content length of data being received.
ContentType Read-write

When overridden in a derived class, gets or sets the content type of the data being received.
Headers Read-only

When overridden in a derived class, gets a collection of header name-value pairs associated with this request.
ResponseUri Read-only

When overridden in a derived class, gets the URI of the Internet resource that actually responded to the request.
Public Methods
Close When overridden by a descendent class, 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
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
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.
GetResponseStream When overridden in a descendant class, returns the data stream from the Internet resource.
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.
Protected Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the WebResponse class.
ctor #2 Overloaded:
.ctor(SerializationInfo serializationInfo, StreamingContext streamingContext)

Initializes a new instance of the WebResponse class from the specified instances of the SerializationInfo and StreamingContext classes.
Protected Methods
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:


System.Net.WebResponse Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the WebResponse class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
protected WebResponse();
Exceptions
Exception Type Condition
NotSupportedException Any attempt is made to access the constructor, when the constructor is not overridden in a descendant class.
Remarks
Applications do not call the WebResponse constructor directly, use the WebRequest.GetResponse method on a WebRequest instance.

Notes to implementors: This constructor creates and initializes the required fields for descendant classes.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the WebResponse class from the specified instances of the SerializationInfo and StreamingContext classes.
C# Syntax:
protected WebResponse(
   SerializationInfo serializationInfo,
   StreamingContext streamingContext
);
Parameters:

serializationInfo

An instance of the SerializationInfo class containing the information required to serialize the new WebRequest instance.

streamingContext

An instance of the StreamingContext class indicating the source of the serialized stream associated with the new WebRequest instance.

Exceptions
Exception Type Condition
NotSupportedException Any attempt is made to access the constructor, when the constructor is not overridden in a descendant class.
Remarks
When implemented by a descendant class, this constructor implements the ISerializable interface for the WebResponse descendant.

Return to top


Property: ContentLength (read-write)
Summary
When overridden in a descendant class, gets or sets the content length of data being received.
C# Syntax:
public virtual long ContentLength {get; set;}
Exceptions
Exception Type Condition
NotSupportedException Any attempt is made to get or set the property, when the property is not overridden in a descendant class.
Remarks
The WebResponse.ContentLength property contains the length, in bytes, of the response from the Internet resource. For request methods that contain header information, the WebResponse.ContentLength does not include the length of the header information.

Note The WebResponse class is an abstract class. The actual behavior of WebResponse instances at run time is determined by the descendent class returned by WebRequest.GetResponse. For more information about default values and exceptions, please see the documentation for the descendent classes, such as HttpWebResponse and FileWebResponse.
See also:
HttpWebResponse.ContentLength

Return to top


Property: ContentType (read-write)
Summary
When overridden in a derived class, gets or sets the content type of the data being received.
C# Syntax:
public virtual string ContentType {get; set;}
Exceptions
Exception Type Condition
NotSupportedException Any attempt is made to get or set the property, when the property is not overridden in a descendant class.
Remarks
The WebResponse.ContentType property contains the MIME content type of the response from the Internet resource, if known.

Note The WebResponse class is an abstract class. The actual behavior of WebResponse instances at run time is determined by the descendent class returned by WebRequest.GetResponse. For more information about default values and exceptions, please see the documentation for the descendent classes, such as HttpWebResponse and FileWebResponse.
See also:
HttpWebResponse.ContentType

Return to top


Property: Headers (read-only)
Summary
When overridden in a derived class, gets a collection of header name-value pairs associated with this request.
C# Syntax:
public virtual WebHeaderCollection Headers {get;}
Exceptions
Exception Type Condition
NotSupportedException Any attempt is made to get or set the property, when the property is not overridden in a descendant class.
Remarks
The WebResponse.Headers property contains the name-value header pairs returned in the response.

Note The WebResponse class is an abstract class. The actual behavior of WebResponse instances at run time is determined by the descendent class returned by WebRequest.GetResponse. For more information about default values and exceptions, please see the documentation for the descendent classes, such as HttpWebResponse and FileWebResponse.
See also:
WebHeaderCollection | HttpWebResponse.Headers

Return to top


Property: ResponseUri (read-only)
Summary
When overridden in a derived class, gets the URI of the Internet resource that actually responded to the request.
C# Syntax:
public virtual Uri ResponseUri {get;}
Exceptions
Exception Type Condition
NotSupportedException Any attempt is made to get or set the property, when the property is not overridden in a descendant class.
Remarks
The WebResponse.ResponseUri property contains the URI of the Internet resource that actually provided the response data. This resource may not be the originally requested URI if the underlying protocol allows redirection of the request.

Note The WebResponse class is an abstract class. The actual behavior of WebResponse instances at run time is determined by the descendent class returned by WebRequest.GetResponse. For more information about default values and exceptions, please see the documentation for the descendent classes, such as HttpWebResponse and FileWebResponse.

Return to top


Method: Close()
Summary
When overridden by a descendent class, closes the response stream.
C# Syntax:
public virtual void Close();
Exceptions
Exception Type Condition
NotSupportedException Any attempt is made to access the method, when the method is not overridden in a descendant class.
Remarks
The WebResponse.Close method cleans up the resources used by a WebResponse and closes the underlying stream by calling the Stream.Close method.

Note The response must be closed to avoid running out of system resources. The response stream can be closed by calling Stream.Close or WebResponse.Close.

Note The WebResponse class is an abstract class. The actual behavior of WebResponse instances at run time is determined by the descendent class returned by WebRequest.GetResponse. For more information about default values and exceptions, please see the documentation for the descendent classes, such as HttpWebResponse and FileWebResponse.

[behaviors: This method is required to free the connection used by the System.IO.Stream returned by a call to System.Net.WebResponse.GetResponseStream.] [default: This method throws System.NotSupportedException.] [overrides: This method is required to be overridden in derived classes to close the response stream and free resources allocated by the current instance.] [usage: Use this method or System.IO.Stream.Close to clean up the resources used by a System.Net.WebResponse.]

See also:
MSDN: usingstreamsonnetwork

Return to top


Method: CreateObjRef(
   Type requestedType
)
Inherited
See base class member description: System.MarshalByRefObject.CreateObjRef

Summary
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual ObjRef CreateObjRef(
   Type requestedType
);
Parameters:

requestedType

The Type of the object that the new ObjRef will reference.

Return Value:
Information required to generate a proxy.
Exceptions
Exception Type Condition
RemotingException This instance is not a valid remoting object.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~WebResponse();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.GetLifetimeService

Summary
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
public object GetLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance.
Remarks
For more information about lifetime services, see the LifetimeServices class.

Return to top


Method: GetResponseStream()
Summary
When overridden in a descendant class, returns the data stream from the Internet resource.
C# Syntax:
public virtual Stream GetResponseStream();
Return Value:
An instance of the Stream class for reading data from the Internet resource.
Exceptions
Exception Type Condition
NotSupportedException Any attempt is made to access the method, when the method is not overridden in a descendant class.
Remarks
The WebResponse.GetResponseStream method returns the data stream from the Internet resource.

Note The response stream must be closed to avoid running out of system resources. The response stream can be closed by calling Stream.Close or WebResponse.Close
See also:
MSDN: usingstreamsonnetwork

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InitializeLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.InitializeLifetimeService

Summary
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual object InitializeLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LifetimeServices.LeaseManagerPollTime property.
Remarks
For more information about lifetime services, see the LifetimeServices class.
Example
The following code example demonstrates creating a lease.
 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;
   }
 }

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.