System.Net.WebResponse Class

public abstract class WebResponse : MarshalByRefObject, IDisposable

Base Types

Object
  MarshalByRefObject
    WebResponse

This type implements IDisposable.

Assembly

System

Library

Networking

Summary

Represents a response received from a Uniform Resource Identifier (URI).

Description

WebResponse is the base class from which protocol-specific response classes, such as HttpWebResponse , are derived.

Classes that derive from WebResponse are required to override the following members in the WebResponse class:

[Note: Applications can participate in request/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.

Applications do not create WebResponse objects directly; they are created by calling System.Net.WebRequest.GetResponse .

]

Example

The following example creates a WebResponse instance from a WebRequest .

using System;
using System.Net;

public class WebResponseExample {

  public static void Main() {

    // 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

Members

WebResponse Constructors

WebResponse Constructor

WebResponse Methods

WebResponse.Close Method
WebResponse.GetResponseStream Method
WebResponse.System.IDisposable.Dispose Method

WebResponse Properties

WebResponse.ContentLength Property
WebResponse.ContentType Property
WebResponse.Headers Property
WebResponse.ResponseUri Property


WebResponse Constructor

protected WebResponse();

Summary

Constructs a new instance of the WebResponse class.

Description

This constructor is called only by classes that derive from WebResponse.

[Note: To obtain a WebResponse instance, use the System.Net.WebRequest.GetResponse method; do not use this constructor.]

See Also

System.Net.WebResponse Class, System.Net Namespace

WebResponse.Close Method

public virtual void Close();

Summary

Closes the response stream.

Exceptions

Exception TypeCondition
NotSupportedExceptionThis method is not implemented in the derived class.

Description

[Behaviors: This method is required to free the connection used by the Stream returned by a call to System.Net.WebResponse.GetResponseStream .]

[Default: The WebResponse class is abstract and does not provide an implementation for this method. This method throws 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 WebResponse .]

See Also

System.Net.WebResponse Class, System.Net Namespace

WebResponse.GetResponseStream Method

public virtual Stream GetResponseStream();

Summary

Returns a Stream that contains data from the current host.

Return Value

A Stream for reading data from the current host.

Exceptions

Exception TypeCondition
NotSupportedExceptionThis method is not implemented in the derived class.

Description

[Behaviors: This method is required to return a Stream from the host specified by the System.Net.WebResponse.ResponseUri property of the current instance.]

[Default: This method throws NotSupportedException.]

[Overrides: This method is required to be overridden by classes that inherit from WebResponse.]

[Usage: Use this method to open a connection to the current host. To close the stream when it is no longer needed, in order to prevent the exhaustion of system resources, invoke either the System.IO.Stream.Close or System.Net.WebResponse.Close methods.]

See Also

System.Net.WebResponse Class, System.Net Namespace

WebResponse.System.IDisposable.Dispose Method

void IDisposable.Dispose();

Summary

Implemented to support the IDisposable interface. [Note: For more information, see System.IDisposable.Dispose.]

See Also

System.Net.WebResponse Class, System.Net Namespace

WebResponse.ContentLength Property

public virtual long ContentLength { get; set; }

Summary

Gets or sets the content length of the data received from the server.

Property Value

A Int64 containing the number of bytes returned from the resource.

Exceptions

Exception TypeCondition
NotSupportedExceptionThis property is not implemented in the derived class.

Description

[Behaviors: This property contains the number of bytes of data in the response to the request associated with the current instance. For request methods that contain header information, the System.Net.WebResponse.ContentLength does not include the length of the header information. The value of this property is set by the Content-Length header received in the response.]

[Default: The WebResponse class is abstract and does not provide an implementation for this property. This property throws NotSupportedException.]

[Overrides: This property is required to be overridden by classes that inherit from WebResponse to set the content length as appropriate for the derived class. ]

[Usage: Use this property to determine the number of bytes in the response.]

See Also

System.Net.WebResponse Class, System.Net Namespace

WebResponse.ContentType Property

public virtual string ContentType { get; set; }

Summary

Gets or sets the media type of the data received.

Property Value

A String that contains the media type of the response data.

Exceptions

Exception TypeCondition
NotSupportedExceptionThis property is not implemented in the derived class.

Description

[Note: The media type is typically the MIME encoding of the content.]

[Behaviors: As described above.]

[Default: The WebResponse class is abstract and does not provide an implementation for this property. This property throws NotSupportedException.]

[Overrides: This property is required to be overridden by classes that inherit from WebResponse.]

[Usage: Use this property to get the media type of the response.]

See Also

System.Net.WebResponse Class, System.Net Namespace

WebResponse.Headers Property

public virtual WebHeaderCollection Headers { get; }

Summary

Gets the collection of header name/value pairs associated with the current instance.

Property Value

A WebHeaderCollection containing the header name/value pairs associated with the current instance.

Exceptions

Exception TypeCondition
NotSupportedExceptionThis property is not implemented in the derived class.

Description

This property contains a WebHeaderCollection instance containing the header information returned in the response from the resource.

[Behaviors: This property is read-only.]

[Default: The WebResponse class is abstract and does not provide an implementation for this property. This property throws NotSupportedException.]

[Overrides: This property is required to be overridden by classes that inherit from WebResponse.]

[Usage: Use this property to retrieve the header information received with the response.]

See Also

System.Net.WebResponse Class, System.Net Namespace

WebResponse.ResponseUri Property

public virtual Uri ResponseUri { get; }

Summary

Gets the URI of the resource associated with the current instance.

Property Value

A Uri containing the URI of the resource associated with the current response.

Exceptions

Exception TypeCondition
NotSupportedExceptionThis property is not implemented in the derived class.

Description

[Behaviors: This property is read-only.

This property returns the URI that provided the response information in the current instance. [Note: If the method protocol used in the associated WebRequest permits redirection, the value of this property might differ from the System.Net.WebRequest.RequestUri property of the request.]

]

[Default: The WebResponse class is abstract and does not provide an implementation for this property. This property throws NotSupportedException .]

[Overrides: This property is required to be overridden by classes that inherit from WebResponse.]

[Usage: Use this property to determine the URI that originated the response.]

See Also

System.Net.WebResponse Class, System.Net Namespace