[Serializable] |
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. |
http://
or
https://
.HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/");
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. |
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. |
ctor #1 | Initializes a new instance of the HttpWebRequest 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. |
ISerializable.GetObjectData | Populates a SerializationInfo with the data needed to serialize the target object. |
Hierarchy:
protected HttpWebRequest( |
serializationInfo
streamingContext
public string Accept {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
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.
public Uri Address {get;}
|
The URI of the original request is kept in the HttpWebRequest.RequestUri property.
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);
public bool AllowAutoRedirect {get; set;}
|
public bool AllowWriteStreamBuffering {get; set;}
|
public string Connection {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | The value of HttpWebRequest.Connection is set to Keep-alive or Close. |
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.
public override string ConnectionGroupName {get; set;}
|
//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();
public override long ContentLength {get; set;}
|
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. |
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.
public override string ContentType {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
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.
public HttpContinueDelegate ContinueDelegate {get; set;}
|
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.
public CookieContainer CookieContainer {get; set;}
|
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.
public override ICredentials Credentials {get; set;}
|
public string Expect {get; set;}
|
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. |
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.
public bool HaveResponse {get;}
|
public override WebHeaderCollection Headers {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
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.
public DateTime IfModifiedSince {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
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.
public bool KeepAlive {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
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.
public int MaximumAutomaticRedirections {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | The value is set to 0 or less. |
public string MediaType {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
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.
public override string Method {get; set;}
|
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. |
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.
public bool Pipelined {get; set;}
|
Pipelined connections are made only when the HttpWebRequest.KeepAlive property is also true.
public override bool PreAuthenticate {get; set;}
|
public Version ProtocolVersion {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | The HTTP version is set to a value other than 1.0 or 1.1. |
public override IWebProxy Proxy {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | HttpWebRequest.Proxy is set to null. |
public string Referer {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
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.
public override Uri RequestUri {get;}
|
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.
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);
public bool SendChunked {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
public ServicePoint ServicePoint {get;}
|
public override int Timeout {get; set;}
|
The HttpWebRequest.Timeout property has no effect on asynchronous requests made with the HttpWebRequest.GetResponse or HttpWebRequest.BeginGetRequestStream methods.
public string TransferEncoding {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | HttpWebRequest.TransferEncoding is set when HttpWebRequest.SendChunked is false. |
ArgumentException | HttpWebRequest.TransferEncoding is set to the value "Chunked". |
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.
public string UserAgent {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The request has been started by calling the HttpWebRequest.GetRequestStream, HttpWebRequest.BeginGetRequestStream, HttpWebRequest.GetResponse, or HttpWebRequest.BeginGetResponse method. |
public override void Abort(); |
public void AddRange( |
range
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.
from
to
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | from is greater than to -or- from or to is less than 0. |
rangeSpecifier
range
Exception Type | Condition |
---|---|
ArgumentNullException | rangeSpecifier is null. |
ArgumentException | rangeSpecifier is invalid. |
InvalidOperationException | The range header could not be added. |
If range is negative, the range is from range to the end of the data.
rangeSpecifier
from
to
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. |
public override IAsyncResult BeginGetRequestStream( |
callback
state
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. |
public override IAsyncResult BeginGetResponse( |
callback
state
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. |
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
public override Stream EndGetRequestStream( |
asyncResult
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. |
public override WebResponse EndGetResponse( |
asyncResult
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. |
~HttpWebRequest(); |
public override int GetHashCode(); |
A
and
B
are guaranteed to be the same when
A.Equals(B)
is true.This method implements the Object.GetHashCode.
public object GetLifetimeService(); |
public override Stream GetRequestStream(); |
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. |
public override WebResponse GetResponse(); |
Exception Type | Condition |
---|---|
WebException | The time-out period for the request expires -or- an error occurs while processing the request. |
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(); |
void ISerializable.GetObjectData( |
serializationInfo
streamingContext
public virtual string ToString(); |