System.Net.HttpWebRequest Class

Assembly: System.dll
Namespace: System.Net
Summary
Provides an HTTP-specific implementation of the WebRequest class.
C# Syntax:
[Serializable]
public class HttpWebRequest : WebRequest
Remarks
The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.

Do not use the HttpWebRequest.#ctor constructor. Use the WebRequest.Create method to initialize new HttpWebRequest instances. If the scheme for the URI is http:// or https:// , WebRequest.Create returns an HttpWebRequest instance.

The HttpWebRequest.GetResponse method makes a synchronous request to the Internet resource specified in the HttpWebRequest.RequestUri property and returns an HttpWebResponse instance containing the response. You can make an asynchronous request to the Internet resource using the HttpWebRequest.BeginGetResponse and HttpWebRequest.EndGetResponse methods.

When you want to send data to the Internet resource, the HttpWebRequest.GetRequestStream method returns a Stream instance to use to send data. The HttpWebRequest.BeginGetRequestStream and HttpWebRequest.EndGetRequestStream methods provide asynchronous access to the send data stream.

The HttpWebRequest 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 HttpWebResponse received from the Internet resource.

HttpWebRequest exposes common HTTP header values sent to the Internet resource as properties, set by methods, or set by the system; the following table contains a complete list. You can set other headers in the HttpWebRequest.Headers property as name/value pairs.

The following table lists the HTTP headers that are set either by properties or methods or the system.



Header Set by
Accept Set by the HttpWebRequest.Accept property.
Connection Set by the HttpWebRequest.Connection property, HttpWebRequest.KeepAlive property.
Content-Length Set by the HttpWebRequest.ContentLength property.
Content-Type Set by the HttpWebRequest.ContentType property.
Expect Set by the HttpWebRequest.Expect property.
Date Set by the system to current date.
Host Set by the system to current host information.
If-Modified-Since Set by the HttpWebRequest.IfModifiedSince property.
Range Set by the HttpWebRequest.AddRange method.
Referer Set by the HttpWebRequest.Referer property.
Transfer-Encoding Set by the HttpWebRequest.TransferEncoding property or the HttpWebRequest.SendChunked property.
User-Agent Set by the HttpWebRequest.UserAgent property.


Note HttpWebRequest is registered automatically. You do not need to call WebRequest.RegisterPrefix to register HttpWebRequest before using URIs beginning with http:// or https:// .
Example
The following example creates an HttpWebRequest for the URI http://www.contoso.com/ .

 HttpWebRequest myReq =
 (HttpWebRequest)WebRequest.Create("http://www.contoso.com/");
 

    
See also:
System.Net Namespace

System.Net.HttpWebRequest Member List:

Public Properties
Accept Read-write

Gets or sets the value of the Accept HTTP header.
Address Read-only

Gets the URI of the Internet resource that actually responds to the request.
AllowAutoRedirect Read-write

Gets or sets a value that indicates whether the request should follow redirection responses.
AllowWriteStreamBuffering Read-write

Gets or sets a value that indicates whether to buffer the data sent to the Internet resource.
Connection Read-write

Gets or sets the value of the Connection HTTP header.
ConnectionGroupName Read-write

Overridden:
Gets or sets the name of the connection group for the request.
ContentLength Read-write

Overridden:
Gets or sets the Content-length HTTP header.
ContentType Read-write

Overridden:
Gets or sets the value of the Content-type HTTP header.
ContinueDelegate Read-write

Gets or sets the delegate method called when an HTTP 100-continue response is received from the Internet resource.
CookieContainer Read-write

Gets or sets the cookies associated with the request.
Credentials Read-write

Overridden:
Provides authentication information for the request.
Expect Read-write

Gets or sets the value of the Expect HTTP header.
HaveResponse Read-only

Gets a value indicating whether a response has been received from an Internet resource.
Headers Read-write

Overridden:
Gets a collection of the name/value pairs that make up the HTTP headers.
IfModifiedSince Read-write

Gets or sets the value of the If-Modified-Since HTTP header.
KeepAlive Read-write

Gets or sets a value indicating whether to make a persistent connection to the Internet resource.
MaximumAutomaticRedirections Read-write

Gets or sets the maximum number of redirects that the request will follow.
MediaType Read-write

Gets or sets the media type of the request.
Method Read-write

Overridden:
Gets or sets the method for the request.
Pipelined Read-write

Gets or sets a value indicating whether to pipeline the request to the Internet resource.
PreAuthenticate Read-write

Overridden:
Gets or sets a value indicating whether to send a preauthentication header with the request.
ProtocolVersion Read-write

Gets or sets the version of HTTP to use for the request.
Proxy Read-write

Overridden:
Gets or sets proxy information for the request.
Referer Read-write

Gets or sets the value of the Referer HTTP header.
RequestUri Read-only

Overridden:
Gets the original URI of the request.
SendChunked Read-write

Gets or sets a value indicating whether to send data in segments to the Internet resource.
ServicePoint Read-only

Gets the service point to use for the request.
Timeout Read-write

Overridden:
Gets or sets the time-out value for a request.
TransferEncoding Read-write

Gets or sets the value of the Transfer-encoding HTTP header.
UserAgent Read-write

Gets or sets the value of the User-agent HTTP header.
Public Methods
Abort Overridden:
Cancels a request to an Internet resource.
AddRange Overloaded:
AddRange(int range)

Adds a byte range header to a request for a specific range from the beginning or end of the requested data.
AddRange Overloaded:
AddRange(int from, int to)

Adds a byte range header to the request for a specified range.
AddRange Overloaded:
AddRange(string rangeSpecifier, int range)

Adds a range header to a request for a specific range from the beginning or end of the requested data.
AddRange Overloaded:
AddRange(string rangeSpecifier, int from, int to)

Adds a range header to a request for a specified range.
BeginGetRequestStream Overridden:
Begins an asynchronous request for a Stream instance to use to write data.
BeginGetResponse Overridden:
Begins an asynchronous request to an Internet resource.
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 Overridden:
Ends an asynchronous request for a Stream instance to use to write data.
EndGetResponse Overridden:
Ends an asynchronous request to an Internet resource..
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 Overridden:
Gets the hash code for this HttpWebRequest.
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 Overridden:
Gets a Stream instance to use to write request data.
GetResponse Overridden:
Returns a response from an Internet resource.
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.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #1 Initializes a new instance of the HttpWebRequest class from the specified instances of the SerializationInfo and StreamingContext classes.
Protected Methods
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.
Explicit Interface Implementations 
ISerializable.GetObjectData Populates a SerializationInfo with the data needed to serialize the target object.

Hierarchy:


System.Net.HttpWebRequest Member Details

ctor #1
Summary
Initializes a new instance of the HttpWebRequest class from the specified instances of the SerializationInfo and StreamingContext classes.
C# Syntax:
protected HttpWebRequest(
   SerializationInfo serializationInfo,
   StreamingContext streamingContext
);
Parameters:

serializationInfo

A SerializationInfo instance containing the information required to serialize the new HttpWebRequest instance.

streamingContext

A StreamingContext instance containing the source and destination of the serialized stream associated with the new HttpWebRequest instance.

Remarks
This constructor implements the ISerializable interface for the HttpWebRequest class.
See also:
MSDN: serialization

Return to top


Property: Accept (read-write)
Summary
Gets or sets the value of the Accept HTTP header.
C# Syntax:
public string Accept {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
To clear the Accept HTTP header, set the HttpWebRequest.Accept property to null.

Changing the HttpWebRequest.Accept property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Property: Address (read-only)
Summary
Gets the URI of the Internet resource that actually responds to the request.
C# Syntax:
public Uri Address {get;}
Remarks
The HttpWebRequest.Address property is set to the URI that actually responds to a request, after any redirections that might happen during the request are complete.

The URI of the original request is kept in the HttpWebRequest.RequestUri property.

Example
The following example checks to see if the HttpWebRequest instance req was redirected to another location to fulfill the request, and sets the value of the hasChanged variable to true if the request was redirected; otherwise hasChanged is set to false.
bool hasChanged = (req.RequestUri != req.Address);
 

    

Return to top


Property: AllowAutoRedirect (read-write)
Summary
Gets or sets a value that indicates whether the request should follow redirection responses.
C# Syntax:
public bool AllowAutoRedirect {get; set;}
Remarks
Set HttpWebRequest.AllowAutoRedirect to true if you want the request to automatically follow HTTP redirection headers to the new location of the resource. The maximum number of redirections to follow is set by the HttpWebRequest.MaximumAutomaticRedirections property.

Return to top


Property: AllowWriteStreamBuffering (read-write)
Summary
Gets or sets a value that indicates whether to buffer the data sent to the Internet resource.
C# Syntax:
public bool AllowWriteStreamBuffering {get; set;}
Remarks
When HttpWebRequest.AllowWriteStreamBuffering is true, the data is buffered in memory so it is ready to be resent in the event of redirections or authentication requests.

Notes to implementors: Setting HttpWebRequest.AllowWriteStreamBufferingtrue might cause performance problems when uploading large datasets because the data buffer could use all available memory.

Return to top


Property: Connection (read-write)
Summary
Gets or sets the value of the Connection HTTP header.
C# Syntax:
public string Connection {get; set;}
Exceptions
Exception Type Condition
ArgumentException The value of HttpWebRequest.Connection is set to Keep-alive or Close.
Remarks
The request sends the HttpWebRequest.Connection property to the Internet resource as the Connection HTTP header. If HttpWebRequest.KeepAlive is true, the value "Keep-alive" is appended to the end of the Connection header.

To clear the Connection HTTP header, set the HttpWebRequest.Connection property to null.

Changing the HttpWebRequest.Connection property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Overridden Property: ConnectionGroupName (read-write)
Summary
Gets or sets the name of the connection group for the request.
C# Syntax:
public override string ConnectionGroupName {get; set;}
Remarks
The HttpWebRequest.ConnectionGroupName property enables you to associate a request with a connection group. This is useful when your application makes requests to one server for different users, such as a Web site that retrieves customer information from a database server.

Notes to implementors: Each connection group creates additional connections for a server. This may result in exceeding ServicePoint.ConnectionLimit for that server.
Example
The following example show how to use user information to form a connection group, assuming that the variables username, password, and domain are set by the application before this code is called.
 //Create a WebRequest.
 WebRequest WReq = WebRequest.Create("http://www.contoso.com/");
 
 // Set the authentication credentials for the request.
 WReq.Credentials = new NetworkCredential(username, password, domain);
 // Assign the request to a connection group based on the user's name
 WReq.ConnectionGroupName = username;
 
 //Get the response
 WebResponse WResp = WReq.GetResponse();

 //Use the response, then close.
 WResp.Close();
    

    

Return to top


Overridden Property: ContentLength (read-write)
Summary
Gets or sets the Content-length HTTP header.
C# Syntax:
public override long ContentLength {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
ArgumentOutOfRangeException The new HttpWebRequest.ContentLength value is less than 0.
Remarks
The HttpWebRequest.ContentLength property contains the value to send as the Content-length HTTP header with the request.

Any value other than -1 in the HttpWebRequest.ContentLength property indicates that the request will upload data and that only methods that upload data are allowed to be set in the HttpWebRequest.Method property.

Once the HttpWebRequest.ContentLength property is set to a value, that number of bytes must be written to the request stream that is returned by calling HttpWebRequest.GetRequestStream or both HttpWebRequest.BeginGetRequestStream and HttpWebRequest.EndGetRequestStream.

Return to top


Overridden Property: ContentType (read-write)
Summary
Gets or sets the value of the Content-type HTTP header.
C# Syntax:
public override string ContentType {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
The HttpWebRequest.ContentType property contains the media type of the request. Values assigned to the HttpWebRequest.ContentType property replace any existing contents when the request sends the Content-type HTTP header.

To clear the Content-type HTTP header, set the HttpWebRequest.ContentType property to null.

Changing the HttpWebRequest.ContentType property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Property: ContinueDelegate (read-write)
Summary
Gets or sets the delegate method called when an HTTP 100-continue response is received from the Internet resource.
C# Syntax:
public HttpContinueDelegate ContinueDelegate {get; set;}
Remarks
The HttpWebRequest.ContinueDelegate property specifies the callback method to call when the client receives a 100-Continue response.

When the HttpWebRequest.ContinueDelegate property is set, the client calls the delegate whenever protocol responses of type HttpStatusCode.Continue (100) are received. This is useful if you want the client to display the status of the data being received from the Internet resource.

Return to top


Property: CookieContainer (read-write)
Summary
Gets or sets the cookies associated with the request.
C# Syntax:
public CookieContainer CookieContainer {get; set;}
Remarks
The HttpWebRequest.CookieContainer property provides an instance of the CookieContainer class that contains the cookies associated with this request.

HttpWebRequest.CookieContainer is null by default. You must assign a CookieContainer instance to the property to have cookies returned in the HttpWebResponse.Cookies property of the HttpWebResponse returned by HttpWebRequest.GetResponse.

See also:
CookieContainer

Return to top


Overridden Property: Credentials (read-write)
Summary
Provides authentication information for the request.
C# Syntax:
public override ICredentials Credentials {get; set;}
Remarks
The HttpWebRequest.Credentials property contains authentication information to identify the maker of the request. The HttpWebRequest.Credentials property can be either an instance of NetworkCredential, in which case the user, password, and domain information contained in the NetworkCredential instance is used to authenticate the request, or it can be an instance of CredentialCache, in which case the uniform resource identifier (URI) of the request is used to determine the user, password, and domain information to use to authenticate the request.

Return to top


Property: Expect (read-write)
Summary
Gets or sets the value of the Expect HTTP header.
C# Syntax:
public string Expect {get; set;}
Exceptions
Exception Type Condition
ArgumentException Expect is set to "100-continue".
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
By default, HttpWebRequest.Expect is set to "100-continue". You can add other values to the list that HttpWebRequest.Expect maintains, or you can delete all values except "100-continue" from the list by setting HttpWebRequest.Expect to null.

Changing the HttpWebRequest.Expect property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Property: HaveResponse (read-only)
Summary
Gets a value indicating whether a response has been received from an Internet resource.
C# Syntax:
public bool HaveResponse {get;}

Return to top


Overridden Property: Headers (read-write)
Summary
Gets a collection of the name/value pairs that make up the HTTP headers.
C# Syntax:
public override WebHeaderCollection Headers {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
The HttpWebRequest.Headers collection contains the protocol headers associated with the request. The following table lists the HTTP headers that are not stored in the HttpWebRequest.Headers collection but are either set by the system or set by properties or methods.

Header Set by
Accept Set by the HttpWebRequest.Accept property.
Connection Set by the HttpWebRequest.Connection property and HttpWebRequest.KeepAlive property.
Content-Length Set by the HttpWebRequest.ContentLength property.
Content-Type Set by the HttpWebRequest.ContentType property.
Expect Set by the HttpWebRequest.Expect property.
Date Set by the system to the current date.
Host Set by the system to the current host information.
If-Modified-Since Set by the HttpWebRequest.IfModifiedSince property.
Range Set by the HttpWebRequest.AddRange method.
Referer Set by the HttpWebRequest.Referer property.
Transfer-Encoding Set by the HttpWebRequest.TransferEncoding property or the HttpWebRequest.SendChunked property.
User-Agent Set by the HttpWebRequest.UserAgent property.

The WebHeaderCollection.Add method throws an ArgumentException if you set one of these protected headers.

Changing the HttpWebRequest.Headers property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Property: IfModifiedSince (read-write)
Summary
Gets or sets the value of the If-Modified-Since HTTP header.
C# Syntax:
public DateTime IfModifiedSince {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
The HttpWebRequest.IfModifiedSince property is assumed to be local time.

Changing the HttpWebRequest.IfModifiedSince property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Property: KeepAlive (read-write)
Summary
Gets or sets a value indicating whether to make a persistent connection to the Internet resource.
C# Syntax:
public bool KeepAlive {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
Set this property to true to send an Connection HTTP header with the value Keep-alive. An application uses HttpWebRequest.KeepAlive to indicate a preference for persistent connections. When the HttpWebRequest.KeepAlive property is true, the application makes persistent connections to the servers that support them.

Note When using HTTP/1.1, Keep-Alive is on by default. Setting HttpWebRequest.KeepAlive false may result in sending a Connection: Close header to the server.

Changing the HttpWebRequest.KeepAlive property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Property: MaximumAutomaticRedirections (read-write)
Summary
Gets or sets the maximum number of redirects that the request will follow.
C# Syntax:
public int MaximumAutomaticRedirections {get; set;}
Exceptions
Exception Type Condition
ArgumentException The value is set to 0 or less.
Remarks
The HttpWebRequest.MaximumAutomaticRedirections method property sets the maximum number of redirections for the request to follow if the HttpWebRequest.AllowAutoRedirect property is true.

Return to top


Property: MediaType (read-write)
Summary
Gets or sets the media type of the request.
C# Syntax:
public string MediaType {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
The value of the HttpWebRequest.MediaType property affects the HttpWebResponse.CharacterSet property. When you set the HttpWebRequest.MediaType in the request, the corresponding media type is chosen from the list of character sets returned in the response Content-type HTTP header.

Changing the HttpWebRequest.MediaType property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Overridden Property: Method (read-write)
Summary
Gets or sets the method for the request.
C# Syntax:
public override string Method {get; set;}
Exceptions
Exception Type Condition
ArgumentException No method is supplied

-or-

the method string contains invalid characters.

InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
The HttpWebRequest.Method property can be set to any of the HTTP 1.1 protocol verbs: GET, HEAD, POST, PUT, DELETE, TRACE, or OPTIONS.

If the HttpWebRequest.ContentLength property is set to any value other than -1, the HttpWebRequest.Method property must be set to a protocol property that uploads data.

Changing the HttpWebRequest.Method property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Property: Pipelined (read-write)
Summary
Gets or sets a value indicating whether to pipeline the request to the Internet resource.
C# Syntax:
public bool Pipelined {get; set;}
Remarks
An application uses the HttpWebRequest.Pipelined property to indicate a preference for pipelined connections. When HttpWebRequest.Pipelined is true, an application makes pipelined connections to the servers that support them.

Pipelined connections are made only when the HttpWebRequest.KeepAlive property is also true.

Return to top


Overridden Property: PreAuthenticate (read-write)
Summary
Gets or sets a value indicating whether to send a preauthentication header with the request.
C# Syntax:
public override bool PreAuthenticate {get; set;}
Remarks
When HttpWebRequest.PreAuthenticate is true and credentials are supplied, the WWW-authenticate HTTP header is sent with the initial request if its value is known; otherwise the request uses standard authentication procedures. If the authentication scheme does not support preauthentication, this property is ignored.

Return to top


Property: ProtocolVersion (read-write)
Summary
Gets or sets the version of HTTP to use for the request.
C# Syntax:
public Version ProtocolVersion {get; set;}
Exceptions
Exception Type Condition
ArgumentException The HTTP version is set to a value other than 1.0 or 1.1.
Remarks
The HttpWebRequest class supports only versions 1.0 and 1.1 of HTTP. Setting HttpWebRequest.ProtocolVersion to a different version throws an exception.

Note To set the HTTP version of the current request, use the HttpVersion.Version10 and HttpVersion.Version11 fields of the HttpVersion class.

Return to top


Overridden Property: Proxy (read-write)
Summary
Gets or sets proxy information for the request.
C# Syntax:
public override IWebProxy Proxy {get; set;}
Exceptions
Exception Type Condition
ArgumentNullException HttpWebRequest.Proxy is set to null.
Remarks
The HttpWebRequest.Proxy property identifies the WebProxy instance to use to process requests to Internet resources. To specify that no proxy should be used, set the HttpWebRequest.Proxy property to the proxy instance returned by the GlobalProxySelection.GetEmptyWebProxy method.
.NET Framework Security:
WebPermission to get or set the HttpWebRequest.Proxy property. Associated enumeration: PermissionState.Unrestricted.

Return to top


Property: Referer (read-write)
Summary
Gets or sets the value of the Referer HTTP header.
C# Syntax:
public string Referer {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
If the HttpWebRequest.AllowAutoRedirect property is true, the HttpWebRequest.Referer property is set automatically when the request is redirected to another site.

To clear the Referer HTTP header, set the HttpWebRequest.Referer property to null.

Changing the HttpWebRequest.Referer property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Overridden Property: RequestUri (read-only)
Summary
Gets the original URI of the request.
C# Syntax:
public override Uri RequestUri {get;}
Remarks
The Uri instance passed to HttpWebRequest by the call to WebRequest.Create.

Following a redirection header does not change the HttpWebRequest.RequestUri property. To get the actual URI that responded to the request, examine the HttpWebRequest.Address property.

Example
The following example checks to see if the HttpWebRequest instance req was redirected to another location to fulfill the request, and sets the value of the hasChanged variable to true if the request was redirected; otherwise hasChanged is set to false.
bool hasChanged = (req.RequestUri != req.Address);
 

    

Return to top


Property: SendChunked (read-write)
Summary
Gets or sets a value indicating whether to send data in segments to the Internet resource.
C# Syntax:
public bool SendChunked {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
When HttpWebRequest.SendChunked is true, the request sends data to the Internet resource in segments. The Internet resource must support receiving chunked data.

Return to top


Property: ServicePoint (read-only)
Summary
Gets the service point to use for the request.
C# Syntax:
public ServicePoint ServicePoint {get;}
Remarks
The value of the HttpWebRequest.ServicePoint property is null until the HttpWebRequest.GetResponse method is called. The ServicePoint.Address property may be different from HttpWebRequest.Address if the request is redirected.

Return to top


Overridden Property: Timeout (read-write)
Summary
Gets or sets the time-out value for a request.
C# Syntax:
public override int Timeout {get; set;}
Remarks
HttpWebRequest.Timeout is the number of milliseconds that a synchronous request made with the HttpWebRequest.GetResponse method waits for a response, and the HttpWebRequest.GetRequestStream methods waits for a stream. If a resource does not respond within the time-out period, the request throws a WebException with the WebException.Status property set to WebExceptionStatus.Timeout.

The HttpWebRequest.Timeout property has no effect on asynchronous requests made with the HttpWebRequest.GetResponse or HttpWebRequest.BeginGetRequestStream methods.

Return to top


Property: TransferEncoding (read-write)
Summary
Gets or sets the value of the Transfer-encoding HTTP header.
C# Syntax:
public string TransferEncoding {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException HttpWebRequest.TransferEncoding is set when HttpWebRequest.SendChunked is false.
ArgumentException HttpWebRequest.TransferEncoding is set to the value "Chunked".
Remarks
Before you can set the HttpWebRequest.TransferEncoding property, you must first set the HttpWebRequest.SendChunked property to true. Clearing HttpWebRequest.TransferEncoding by setting it to null has no effect on the value of HttpWebRequest.SendChunked.

Values assigned to the HttpWebRequest.TransferEncoding property replace any existing contents.

Changing the HttpWebRequest.TransferEncoding property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Property: UserAgent (read-write)
Summary
Gets or sets the value of the User-agent HTTP header.
C# Syntax:
public string UserAgent {get; set;}
Exceptions
Exception Type Condition
InvalidOperationException The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method.
Remarks
Changing the HttpWebRequest.UserAgent property after the request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method will throw an InvalidOperationException.

Return to top


Overridden Method: Abort()
Summary
Cancels a request to an Internet resource.
C# Syntax:
public override void Abort();
Remarks
HttpWebRequest.Abort cancels a request to a resource. After a request is canceled, calling HttpWebRequest.GetResponse, HttpWebRequest.BeginGetResponse, HttpWebRequest.EndGetResponse, HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, or HttpWebRequest.EndGetRequestStream will cause a WebException with WebException.Status set to WebExceptionStatus.RequestCanceled.

Return to top


Overloaded Method: AddRange(
   int range
)
Summary
Adds a byte range header to a request for a specific range from the beginning or end of the requested data.
C# Syntax:
public void AddRange(
   int range
);
Parameters:

range

The starting or ending point of the range.

Remarks
HttpWebRequest.AddRange adds a byte range header to the request.

If range is positive, the range is from the start of the data to range.

If range is negative, the range is from range to the end of the data.

Return to top


Overloaded Method: AddRange(
   int from,
   int to
)
Summary
Adds a byte range header to the request for a specified range.
C# Syntax:
public void AddRange(
   int from,
   int to
);
Parameters:

from

The position at which to start sending data.

to

The position at which to stop sending data.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException from is greater than to

-or-

from or to is less than 0.

Remarks
HttpWebRequest.AddRange adds a byte range header to the request.

Return to top


Overloaded Method: AddRange(
   string rangeSpecifier,
   int range
)
Summary
Adds a range header to a request for a specific range from the beginning or end of the requested data.
C# Syntax:
public void AddRange(
   string rangeSpecifier,
   int range
);
Parameters:

rangeSpecifier

The description of the range.

range

The starting or ending point of the range.

Exceptions
Exception Type Condition
ArgumentNullException rangeSpecifier is null.
ArgumentException rangeSpecifier is invalid.
InvalidOperationException The range header could not be added.
Remarks
If range is positive, the range is from the start of the data to range.

If range is negative, the range is from range to the end of the data.

Return to top


Overloaded Method: AddRange(
   string rangeSpecifier,
   int from,
   int to
)
Summary
Adds a range header to a request for a specified range.
C# Syntax:
public void AddRange(
   string rangeSpecifier,
   int from,
   int to
);
Parameters:

rangeSpecifier

The description of the range.

from

The position at which to start sending data.

to

The position at which to stop sending data.

Exceptions
Exception Type Condition
ArgumentNullException rangeSpecifier is null.
ArgumentOutOfRangeException from is greater than to

-or-

from or to is less than 0.

ArgumentException rangeSpecifier is invalid.
InvalidOperationException The range header could not be added.

Return to top


Overridden Method: BeginGetRequestStream(
   AsyncCallback callback,
   object state
)
Summary
Begins an asynchronous request for a Stream instance to use to write data.
C# Syntax:
public override IAsyncResult BeginGetRequestStream(
   AsyncCallback callback,
   object state
);
Parameters:

callback

The AsyncCallback delegate.

state

The state object for this request.

Return Value:
An IAsyncResult that references the asynchronous request.
Exceptions
Exception Type Condition
ProtocolViolationException The HttpWebRequest.Method property is GET and the application writes to the stream.
InvalidOperationException The stream is being used by a previous call to HttpWebRequest.BeginGetRequestStream.
ApplicationException No write stream is available.
Remarks
The HttpWebRequest.BeginGetRequestStream method starts an asynchronous request for a stream used to send data for the HttpWebRequest. The asynchronous callback method uses the HttpWebRequest.EndGetRequestStream method to return the actual stream.

Return to top


Overridden Method: BeginGetResponse(
   AsyncCallback callback,
   object state
)
Summary
Begins an asynchronous request to an Internet resource.
C# Syntax:
public override IAsyncResult BeginGetResponse(
   AsyncCallback callback,
   object state
);
Parameters:

callback

The AsyncCallback delegate

state

The state object for this request.

Return Value:
An IAsyncResult that references the asynchronous request for a response.
Exceptions
Exception Type Condition
InvalidOperationException The stream is already in use by a previous call to HttpWebRequest.BeginGetResponse

-or-

HttpWebRequest.TransferEncoding is set to a value and HttpWebRequest.SendChunked is false.

ProtocolViolationException HttpWebRequest.Method is GET and either HttpWebRequest.ContentLength is greater than zero or HttpWebRequest.SendChunked is true.

-or-

HttpWebRequest.KeepAlive is true, HttpWebRequest.AllowWriteStreamBuffering is false, and either HttpWebRequest.ContentLength is greater than zero or HttpWebRequest.SendChunked is true.

Remarks
The HttpWebRequest.BeginGetResponse method starts an asynchronous request for a response from the Internet resource. The asynchronous callback method uses the HttpWebRequest.EndGetResponse method to return the actual WebResponse.

Return to top


Method: CreateObjRef(
   Type requestedType
)
Inherited
See base class member description: System.MarshalByRefObject.CreateObjRef

Summary
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual ObjRef CreateObjRef(
   Type requestedType
);
Parameters:

requestedType

The Type of the object that the new ObjRef will reference.

Return Value:
Information required to generate a proxy.
Exceptions
Exception Type Condition
RemotingException This instance is not a valid remoting object.

Return to top


Overridden Method: EndGetRequestStream(
   IAsyncResult asyncResult
)
Summary
Ends an asynchronous request for a Stream instance to use to write data.
C# Syntax:
public override Stream EndGetRequestStream(
   IAsyncResult asyncResult
);
Parameters:

asyncResult

The pending request for a stream.

Return Value:
A Stream to use to write request data.
Exceptions
Exception Type Condition
ArgumentNullException asyncResult is null.
IOException The request did not complete, and no stream is available.
WebException An error occurs while processing the request.
Remarks
The HttpWebRequest.EndGetRequestStream method completes an asynchronous request for a stream that was started by the HttpWebRequest.BeginGetRequestStream method. Once the Stream instance has been returned, you can send data with the HttpWebRequest by using the Stream.Write method.

Note You must set the value of the HttpWebRequest.ContentLength property before writing data to the stream. You must call the Stream.Close method to close the stream and release the connection for reuse. Failure to close the stream will cause your application to run out of connections.

Return to top


Overridden Method: EndGetResponse(
   IAsyncResult asyncResult
)
Summary
Ends an asynchronous request to an Internet resource..
C# Syntax:
public override WebResponse EndGetResponse(
   IAsyncResult asyncResult
);
Parameters:

asyncResult

The pending request for a response.

Return Value:
A WebResponse containing the response from the Internet resource.
Exceptions
Exception Type Condition
ArgumentNullException AsyncResult is null.
InvalidOperationException The HttpWebRequest.ContentLength property is greater than 0 but the data has not been written to the request stream.
WebException An error occurs while processing the request.
Remarks
The HttpWebRequest.EndGetResponse method completes an asynchronous request for an Internet resource that was started by calling HttpWebRequest.BeginGetResponse.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~HttpWebRequest();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetHashCode()
Summary
Gets the hash code for this HttpWebRequest.
C# Syntax:
public override int GetHashCode();
Return Value:
The hash code for the HttpWebRequest.
Remarks
The hash codes for HttpWebRequest A and B are guaranteed to be the same when A.Equals(B) is true.

This method implements the Object.GetHashCode.

Return to top


Method: GetLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.GetLifetimeService

Summary
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
public object GetLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance.
Remarks
For more information about lifetime services, see the LifetimeServices class.

Return to top


Overridden Method: GetRequestStream()
Summary
Gets a Stream instance to use to write request data.
C# Syntax:
public override Stream GetRequestStream();
Return Value:
A Stream to use to write request data.
Exceptions
Exception Type Condition
ProtocolViolationException The HttpWebRequest.Method property is GET and the application writes to the stream.
InvalidOperationException The HttpWebRequest.GetRequestStream method is called more than once.
WebException An error occurs while processing the request.
Remarks
The HttpWebRequest.GetRequestStream method returns a stream to use to send data for the HttpWebRequest. Once the Stream instance has been returned, you can send data with the HttpWebRequest by using the Stream.Write method.

Note You must set the value of the HttpWebRequest.ContentLength property before writing data to the stream. You must call the Stream.Close method to close the stream and release the connection for reuse. Failure to close the stream will cause your application to run out of connections.

Return to top


Overridden Method: GetResponse()
Summary
Returns a response from an Internet resource.
C# Syntax:
public override WebResponse GetResponse();
Return Value:
A WebResponse containing the response from the Internet resource.
Exceptions
Exception Type Condition
WebException The time-out period for the request expires

-or-

an error occurs while processing the request.

Remarks
The HttpWebRequest.GetResponse method returns a WebResponse instance containing the response from the Internet resource. The actual instance returned is an instance of HttpWebResponse, and can be typecast to that class to access HTTP-specific properties.
See also:
HttpWebRequest.Timeout

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InitializeLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.InitializeLifetimeService

Summary
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual object InitializeLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LifetimeServices.LeaseManagerPollTime property.
Remarks
For more information about lifetime services, see the LifetimeServices class.
Example
The following code example demonstrates creating a lease.
 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;
   }
 }

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ISerializable.GetObjectData(
   SerializationInfo serializationInfo,
   StreamingContext streamingContext
)
Summary
Populates a SerializationInfo with the data needed to serialize the target object.
C# Syntax:
void ISerializable.GetObjectData(
   SerializationInfo serializationInfo,
   StreamingContext streamingContext
);
Parameters:

serializationInfo

The SerializationInfo to populate with data.

streamingContext

The destination (see StreamingContext) for this serialization.

Implements:
ISerializable.GetObjectData
Remarks
Any objects included in the SerializationInfo are automatically tracked and serialized by the formatter.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.