[Serializable] |
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 method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.
// 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();
ConnectionGroupName | Read-write When overridden in a descendant class, gets or sets the name of the connection group for the request. |
ContentLength | Read-write When overridden in a descendant class, gets or sets the content length of the request data being sent. |
ContentType | Read-write When overridden in a descendant class, gets or sets the content type of the request data being sent. |
Credentials | Read-write When overridden in a descendant class, gets or sets the network credentials used for authenticating the request with the Internet resource. |
Headers | Read-write When overridden in a descendant class, gets or sets the collection of header name/value pairs associated with the request. |
Method | Read-write When overridden in a descendant class, gets or sets the protocol method to use in this request. |
PreAuthenticate | Read-write When overridden in a descendant class, indicates whether to preauthenticate the request. |
Proxy | Read-write When overridden in a descendant class, gets or sets the network proxy to use to access this Internet resource. |
RequestUri | Read-only When overridden in a descendant class, gets the URI of the Internet resource associated with the request. |
Timeout | Read-write Gets or sets the length of time before the request times out. |
Abort | Cancels an asynchronous request to an Internet resource. |
BeginGetRequestStream | When overridden in a descendant class, provides an asynchronous version of the WebRequest.GetRequestStream method. |
BeginGetResponse | When overridden in a descendant class, begins an asynchronous request for an Internet resource. |
Create | Overloaded:Create(string requestUriString) Initializes a new WebRequest instance for the specified URI scheme. |
Create | Overloaded:Create(Uri requestUri) Initializes a new WebRequest instance for the specified URI scheme. |
CreateDefault | Initializes a new WebRequest instance for the specified URI scheme. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
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 (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
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. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
RegisterPrefix | Registers a WebRequest descendant for the specified URI. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the WebRequest class. |
ctor #2 | Overloaded:.ctor(SerializationInfo serializationInfo, StreamingContext streamingContext) Initializes a new instance of the WebRequest class from the specified instances of the SerializationInfo and StreamingContext classes. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
protected WebRequest(); |
WebRequest myRequest = WebRequest.Create("http://www.contoso.com");
protected WebRequest( |
serializationInfo
streamingContext
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to access the constructor, when the constructor is not overridden in a descendant class. |
public virtual string ConnectionGroupName {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual long ContentLength {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual string ContentType {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual ICredentials Credentials {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual WebHeaderCollection Headers {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual string Method {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | If the property is not overridden in a descendant class, any attempt is made to get or set the property. |
public virtual bool PreAuthenticate {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual IWebProxy Proxy {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual Uri RequestUri {get;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual int Timeout {get; set;}
|
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to get or set the property, when the property is not overridden in a descendant class. |
public virtual void Abort(); |
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to access the method, when the method is not overridden in a descendant class. |
public virtual IAsyncResult BeginGetRequestStream( |
callback
state
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to access the method, when the method is not overridden in a descendant class. |
public virtual IAsyncResult BeginGetResponse( |
callback
state
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to access the method, when the method is not overridden in a descendant class. |
public static WebRequest Create( |
requestUriString
Exception Type | Condition |
---|---|
NotSupportedException | The request scheme specified in requestUriString has not been registered. |
ArgumentNullException | requestUriString is null. |
UriFormatException | The URI specified in requestUriString is not a valid URI. |
http://
is passed in requestUriString, an HttpWebRequest instance is returned by WebRequest.Create. If a URI beginning with
file://
is passed instead, the WebRequest.Create method will return a FileWebRequest instance. The .NET Framework includes support for the
http://
,
https://
, and
file://
URI schemes. Custom WebRequest descendants to handle other requests are registered with the WebRequest.RegisterPrefix method.
The WebRequest.Create method uses the requestUriString parameter to create a Uri instance that it passes to the new WebRequest.
public static WebRequest Create( |
requestUri
Exception Type | Condition |
---|---|
NotSupportedException | The request scheme specified in requestUri is not registered. |
ArgumentNullException | requestUri is null. |
http://
is passed in requestUri, an HttpWebRequest instance is returned by WebRequest.Create. If a URI beginning with
file://
is passed instead, the WebRequest.Create method will return a FileWebRequest instance. The .NET Framework includes support for the
http://
,
https://
, and
file://
URI schemes. Custom WebRequest descendants to handle other requests are registered with the WebRequest.RegisterPrefix method.
public static WebRequest CreateDefault( |
requestUri
Exception Type | Condition |
---|---|
ArgumentNullException | requestUri is null. |
For example, if you create a WebRequest descendant, Handler1, to handle requests to http://www.contoso.com/text/ and another named Handler2 to handle requests to http://www.contoso.com/code/, you can use the WebRequest.CreateDefault method to return a default WebRequest instance based on the URI scheme (in this case http).
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
public virtual Stream EndGetRequestStream( |
asyncResult
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to access the method, when the method is not overridden in a descendant class. |
public virtual WebResponse EndGetResponse( |
asyncResult
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to access the method, when the method is not overridden in a descendant class. |
~WebRequest(); |
public virtual int GetHashCode(); |
public object GetLifetimeService(); |
public virtual Stream GetRequestStream(); |
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to access the method, when the method is not overridden in a descendant class. |
The WebRequest.GetRequestStream method provides synchronous access to the Stream. For asynchronous access, use the WebRequest.BeginGetRequestStream and WebRequest.EndGetRequestStream methods.
public virtual WebResponse GetResponse(); |
Exception Type | Condition |
---|---|
NotSupportedException | Any attempt is made to access the method, when the method is not overridden in a descendant class. |
The WebRequest.GetResponse method provides synchronous access to the WebResponse. For asynchronous access, use the WebRequest.BeginGetResponse and WebRequest.EndGetResponse methods.
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
public static bool RegisterPrefix( |
prefix
creator
Exception Type | Condition |
---|---|
ArgumentNullException | prefix is null -or- creator is null. |
Duplicate prefixes are not allowed. WebRequest.RegisterPrefix returns false if an attempt is made to register a duplicate prefix.
public virtual string ToString(); |