Makes a request to a Uniform Resource Identifier (URI). This is an abstract (MustInherit in Visual Basic) class.
For a list of all members of this type, see WebRequest Members.
[Visual Basic] <Serializable> MustInherit Public Class WebRequest Inherits MarshalByRefObject Implements ISerializable [C#] [Serializable] public abstract class WebRequest : MarshalByRefObject, ISerializable [C++] [Serializable] public __gc __abstract class WebRequest : public MarshalByRefObject, ISerializable [JScript] public Serializable abstract class WebRequest extends MarshalByRefObject implements ISerializable
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
WebRequest is the abstract (MustInherit in Visual Basic) 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 Response property contains the WebResponse received from the Internet resource.
Because the WebRequest class is an abstract (MustInherit in Visual Basic) class, the actual behavior of WebRequest instances at run time is determined by the descendant class returned by WebRequest.Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.
Note Use the Create method to initialize new WebRequest instances. Do not use the WebRequest constructor.
Notes to Inheritors: When you inherit from WebRequest, you must override the following members: Method, RequestUri, Headers, ContentLength, ContentType, Credentials, PreAuthenticate, GetRequestStream, BeginGetRequestStream, EndGetRequestStream, GetResponse, BeginGetResponse, and EndGetResponse. In addition, you must provide an implementation of the IWebRequestCreate interface, which defines the Create method used when you call Create. You must register the class that implements the IWebRequestCreate interface, using the RegisterPrefix method or the configuration file.
[Visual Basic, C#, C++] The following example shows how to create a WebRequest instance and return the response.
[Visual Basic] ' Initialize the WebRequest. Dim myRequest As WebRequest = WebRequest.Create("http://www.contoso.com") ' Return the response. Dim myResponse As WebResponse = myRequest.GetResponse() ' Code to use the WebResponse goes here. ' Close the response to free resources. myResponse.Close() [C#] // 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(); [C++] // Initialize the WebRequest. WebRequest* myRequest = WebRequest::Create(S"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();
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System (in System.dll)
.NET Framework Security:
- WebPermission to access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect.