System.Net.WebRequest Class

Makes a request to a Uniform Resource Identifier (URI). This is an abstract class.

See Also: WebRequest Members

Syntax

public abstract class WebRequest : MarshalByRefObject, ISerializable

Remarks

WebRequest is the abstract base class for the .NET Framework's request/response model for accessing data from the Internet. An application that uses the request/response model can request data from the Internet in a protocol-agnostic manner, in which the application works with instances of the WebRequest class while protocol-specific descendant classes carry out the details of the request.

Requests are sent from an application to a particular URI, such as a Web page on a server. The URI determines the proper descendant class to create from a list of WebRequest descendants registered for the application. WebRequest descendants are typically registered to handle a specific protocol, such as HTTP or FTP, but can be registered to handle a request to a specific server or path on a server.

The WebRequest class throws a WebException when errors occur while accessing an Internet resource. The WebException.Status property is one of the WebExceptionStatus values that indicates the source of the error. When WebException.Status is WebExceptionStatus.ProtocolError, the WebException.Response property contains the WebResponse received from the Internet resource.

Because the WebRequest class is an abstract class, the actual behavior of WebRequest instances at run time is determined by the descendant class returned by WebRequest.Create(Uri) method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

Note:

Use the WebRequest.Create(Uri) method to initialize new WebRequest instances. Do not use the WebRequest constructor.

Note:

If the application that creates the WebRequest object runs with the credentials of a Normal user, the application will not be able to access certificates installed in the local machine store unless permission has been explicitly given to the user to do so.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates using WebRequest.Create(Uri, bool) to create an instance of HttpWebRequest .

C# Example

using System;
using System.Net;

public class WebRequestExample {

  public static void Main() {

    // Initialize the WebRequest.
    WebRequest myRequest =
      WebRequest.Create("http://www.contoso.com");

    // Print the type of the request.
    Console.WriteLine(myRequest);
  }
}

The output is

System.Net.HttpWebRequest

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.x.x, 1.0.5000.0, 2.0.0.0, 4.0.0.0

The members of System.Net.WebRequest are listed below.

See Also: Inherited members from System.MarshalByRefObject

Protected Constructors

Initializes a new instance of the WebRequest class.

Initializes a new instance of the WebRequest class from the specified instances of the SerializationInfo and StreamingContext classes.

Public Properties

AuthenticationLevelAuthenticationLevel.

Gets or sets values indicating the level of authentication and impersonation used for this request.

CachePolicyRequestCachePolicy.

Gets or sets the cache policy for this request.

ConnectionGroupNamestring.

When overridden in a descendant class, gets or sets the name of the connection group for the request.

ContentLengthlong.

When overridden in a descendant class, gets or sets the content length of the request data being sent.

ContentTypestring.

When overridden in a descendant class, gets or sets the content type of the request data being sent.

CredentialsICredentials.

When overridden in a descendant class, gets or sets the network credentials used for authenticating the request with the Internet resource.

static
DefaultCachePolicyRequestCachePolicy.

Gets or sets the default cache policy for this request.

static
DefaultWebProxyIWebProxy.

Gets or sets the global HTTP proxy.

HeadersWebHeaderCollection.

When overridden in a descendant class, gets or sets the collection of header name/value pairs associated with the request.

ImpersonationLevelTokenImpersonationLevel.

Gets or sets the impersonation level for the current request.

Methodstring.

When overridden in a descendant class, gets or sets the protocol method to use in this request.

PreAuthenticatebool.

When overridden in a descendant class, indicates whether to pre-authenticate the request.

ProxyIWebProxy.

When overridden in a descendant class, gets or sets the network proxy to use to access this Internet resource.

[read-only]
RequestUriUri.

When overridden in a descendant class, gets the URI of the Internet resource associated with the request.

Timeoutint.

Gets or sets the length of time, in milliseconds, before the request times out.

UseDefaultCredentialsbool.

When overridden in a descendant class, gets or sets a bool value that controls whether CredentialCache.DefaultCredentials are sent with requests.

Public Methods

Abort()

Aborts the Request

BeginGetRequestStream(AsyncCallback, object) : IAsyncResult

When overridden in a descendant class, provides an asynchronous version of the WebRequest.GetRequestStream method.

BeginGetResponse(AsyncCallback, object) : IAsyncResult

When overridden in a descendant class, begins an asynchronous request for an Internet resource.

static
Create(string) : WebRequest

Initializes a new WebRequest instance for the specified URI scheme.

static
Create(Uri) : WebRequest

Initializes a new WebRequest instance for the specified URI scheme.

static
CreateDefault(Uri) : WebRequest

Initializes a new WebRequest instance for the specified URI scheme.

static
CreateHttp(string) : HttpWebRequest

Initializes a new HttpWebRequest instance for the specified URI string.

static
CreateHttp(Uri) : HttpWebRequest

Initializes a new HttpWebRequest instance for the specified URI.

EndGetRequestStream(IAsyncResult) : Stream

When overridden in a descendant class, returns a Stream for writing data to the Internet resource.

EndGetResponse(IAsyncResult) : WebResponse

When overridden in a descendant class, returns a WebResponse.

GetRequestStream() : Stream

When overridden in a descendant class, returns a Stream for writing data to the Internet resource.

GetRequestStreamAsync() : System.Threading.Tasks.Task<System.IO.Stream>

When overridden in a descendant class, returns a Stream for writing data to the Internet resource as an asynchronous operation.

GetResponse() : WebResponse

When overridden in a descendant class, returns a response to an Internet request.

GetResponseAsync() : System.Threading.Tasks.Task<WebResponse>

When overridden in a descendant class, returns a response to an Internet request as an asynchronous operation.

static
GetSystemWebProxy() : IWebProxy

Returns a proxy configured with the Internet Explorer settings of the currently impersonated user.

static
RegisterPrefix(string, IWebRequestCreate) : bool

Registers a WebRequest descendant for the specified URI.

Protected Methods

GetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo with the data needed to serialize the target object.

Explicitly Implemented Interface Members

System.Runtime.Serialization.ISerializable.GetObjectData

When overridden in a descendant class, populates a SerializationInfo instance with the data needed to serialize the WebRequest.