Because many Internet protocols
are request-response synchronous
protocols, this class serves as a base type for any and all
request-response style of network communication. As such, a .NET
programmer will never create a WebRequest type
directly—instead, a static method on this class,
Create( ), is used as a "virtual
constructor" to create a subtype of
WebRequest that matches the protocol scheme
requested. For example, if the string
http://www.oreilly.com is passed to
Create( ), an instance of
HttpWebRequest is handed back. Out of the box,
only "http",
"https", and
"file" are supported.
Once obtained, a .NET programmer can manipulate the common properties
of the WebRequest type to control various aspects
of the request. Alternatively, downcast the generic
WebRequest reference to the concrete type returned
to access protocol-specific aspects of that protocol—for
example, the returned object from
WebRequest.Create("http://www.oreilly.com")
will be a HttpWebRequest, so it is safe to cast it
as such. This allows access to the Accept and
SendChunked properties/headers in the request. Be
sure to manipulate these properties before the request is sent, or
the modifications will have no effect.
Use the GetResponse( ) method to obtain a
WebResponse object corresponding to the response
that the remote server sent. This means that the request is sent, and
the response harvested. The methods BeginGetResponse(
) and EndGetResponse( ) are asynchronous
versions of GetResponse( ).
By default, WebRequest uses the proxy server
specified in GlobalProxySelection.Select. Override
that setting by assigning an IWebProxy
implementation to the Proxy property.
public abstract class WebRequest : MarshalByRefObject, System.Runtime.Serialization.ISerializable {
// Protected Constructors
protected WebRequest( );
protected WebRequest(System.Runtime.Serialization.SerializationInfo serializationInfo,
System.Runtime.Serialization. StreamingContext streamingContext);
// Public Instance Properties
public virtual string ConnectionGroupName{set; get; }
public virtual long ContentLength{set; get; }
public virtual string ContentType{set; get; }
public virtual ICredentials Credentials{set; get; }
public virtual WebHeaderCollection Headers{set; get; }
public virtual string Method{set; get; }
public virtual bool PreAuthenticate{set; get; }
public virtual IWebProxy Proxy{set; get; }
public virtual Uri RequestUri{get; }
public virtual int Timeout{set; get; }
// Public Static Methods
public static WebRequest Create(string requestUriString);
public static WebRequest Create(Uri requestUri);
public static WebRequest CreateDefault(Uri requestUri);
public static bool RegisterPrefix(string prefix, IWebRequestCreate creator);
// Public Instance Methods
public virtual void Abort( );
public virtual IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);
public virtual IAsyncResult BeginGetResponse(AsyncCallback callback, object state);
public virtual Stream EndGetRequestStream(IAsyncResult asyncResult);
public virtual WebResponse EndGetResponse(IAsyncResult asyncResult);
public virtual Stream GetRequestStream( );
public virtual WebResponse GetResponse( );
}