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, System.Runtime.Serialization.ISerializable

Remarks

System.Net.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 System.Net.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 System.Net.WebRequest descendants registered for the application. System.Net.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 System.Net.WebRequest class throws a System.Net.WebException when errors occur while accessing an Internet resource. The WebException.Status property is one of the System.Net.WebExceptionStatus values that indicates the source of the error. When WebException.Status is WebExceptionStatus.ProtocolError, the WebException.Response property contains the System.Net.WebResponse received from the Internet resource.

Because the System.Net.WebRequest class is an abstract class, the actual behavior of System.Net.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 System.Net.HttpWebRequest and System.Net.FileWebRequest.

Note:

Use the WebRequest.Create(Uri) method to initialize new System.Net.WebRequest instances. Do not use the System.Net.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 System.Net.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