Makes a request to a Uniform Resource Identifier (URI). This is an abstract class.
Assembly: System (in System.dll)
Thetype exposes the following members.
|AuthenticationLevel||Gets or sets values indicating the level of authentication and impersonation used for this request.|
|CachePolicy||Gets or sets the cache policy for this request.|
|ConnectionGroupName||When overridden in a descendant class, gets or sets the name of the connection group for the request.|
|ContentLength||When overridden in a descendant class, gets or sets the content length of the request data being sent.|
|ContentType||When overridden in a descendant class, gets or sets the content type of the request data being sent.|
|Credentials||When overridden in a descendant class, gets or sets the network credentials used for authenticating the request with the Internet resource.|
|DefaultCachePolicy||Gets or sets the default cache policy for this request.|
|DefaultWebProxy||Gets or sets the global HTTP proxy.|
|Headers||When overridden in a descendant class, gets or sets the collection of header name/value pairs associated with the request.|
|ImpersonationLevel||Gets or sets the impersonation level for the current request.|
|Method||When overridden in a descendant class, gets or sets the protocol method to use in this request.|
|PreAuthenticate||When overridden in a descendant class, indicates whether to pre-authenticate the request.|
|Proxy||When overridden in a descendant class, gets or sets the network proxy to use to access this Internet resource.|
|RequestUri||When overridden in a descendant class, gets the URI of the Internet resource associated with the request.|
|Timeout||Gets or sets the length of time, in milliseconds, before the request times out.|
|UseDefaultCredentials||When overridden in a descendant class, gets or sets a Boolean value that controls whether DefaultCredentials are sent with requests.|
|Abort||Aborts the Request|
|BeginGetRequestStream||When overridden in a descendant class, provides an asynchronous version of the GetRequestStream method.|
|BeginGetResponse||When overridden in a descendant class, begins an asynchronous request for an Internet resource.|
|Create(String)||Initializes a new instance for the specified URI scheme.|
|Create(Uri)||Initializes a new instance for the specified URI scheme.|
|CreateDefault||Initializes a new instance for the specified URI scheme.|
|CreateObjRef||Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)|
|EndGetRequestStream||When overridden in a descendant class, returns a Stream for writing data to the Internet resource.|
|EndGetResponse||When overridden in a descendant class, returns a WebResponse.|
|Equals(Object)||Determines whether the specified Object is equal to the current Object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Serves as a hash function for a particular type. (Inherited from Object.)|
|GetLifetimeService||Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|GetObjectData||Infrastructure. Populates a SerializationInfo with the data needed to serialize the target object.|
|GetRequestStream||When overridden in a descendant class, returns a Stream for writing data to the Internet resource.|
|GetResponse||When overridden in a descendant class, returns a response to an Internet request.|
|GetSystemWebProxy||Returns a proxy configured with the Internet Explorer settings of the currently impersonated user.|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|InitializeLifetimeService||Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|MemberwiseClone(Boolean)||Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)|
|RegisterPrefix||Registers a descendant for the specified URI.|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
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 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 descendants registered for the application. 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 class throws a WebException when errors occur while accessing an Internet resource. The Status property is one of the WebExceptionStatus values that indicates the source of the error. When Status is WebExceptionStatus.ProtocolError, the Response property contains the WebResponse received from the Internet resource.
Because the class is an abstract class, the actual behavior of instances at run time is determined by the descendant class returned by Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.
The class contains new members when it is used in a Portable Class Library project. For more information, see API Differences in Portable Class Library.
Use the Create method to initialize new instances. Do not use the constructor.
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.
When you inherit from , 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.
The following example shows how to create a instance and return the response.
Imports System Imports System.IO Imports System.Net Imports System.Text Namespace Examples.System.Net Public Class WebRequestGetExample Public Shared Sub Main() ' Create a request for the URL. Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html") ' If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials ' Get the response. Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) ' Display the status. Console.WriteLine(response.StatusDescription) ' Get the stream containing content returned by the server. Dim dataStream As Stream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As New StreamReader(dataStream) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() ' Display the content. Console.WriteLine(responseFromServer) ' Cleanup the streams and the response. reader.Close() dataStream.Close() response.Close() End Sub 'Main End Class 'WebRequestGetExample End Namespace
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.