System.Net.HttpWebRequest Class

Provides an HTTP-specific implementation of the System.Net.WebRequest class.

See Also: HttpWebRequest Members

Syntax

public class HttpWebRequest : WebRequest, System.Runtime.Serialization.ISerializable

Remarks

The System.Net.HttpWebRequest class provides support for the properties and methods defined in System.Net.WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.

Do not use the HttpWebRequest.#ctor(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext) constructor. Use the erload:System.Net.WebRequest.Create method to initialize new System.Net.HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, erload:System.Net.WebRequest.Create returns an System.Net.HttpWebRequest object.

The HttpWebRequest.GetResponse method makes a synchronous request to the resource specified in the HttpWebRequest.RequestUri property and returns an System.Net.HttpWebResponse that contains the response. You can make an asynchronous request to the resource using the HttpWebRequest.BeginGetResponse(AsyncCallback, object) and HttpWebRequest.EndGetResponse(IAsyncResult) methods.

When you want to send data to the resource, the HttpWebRequest.GetRequestStream method returns a System.IO.Stream object to use to send data. The HttpWebRequest.BeginGetRequestStream(AsyncCallback, object) and HttpWebRequest.EndGetRequestStream(IAsyncResult) methods provide asynchronous access to the send data stream.

For client authentication with System.Net.HttpWebRequest, the client certificate must be installed in the My certificate store of the current user.

The System.Net.HttpWebRequest class throws a System.Net.WebException when errors occur while accessing a resource. The WebException.Status property contains a System.Net.WebExceptionStatus value that indicates the source of the error. When WebException.Status is WebExceptionStatus.ProtocolError, the WebException.Response property contains the System.Net.HttpWebResponse received from the resource.

System.Net.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. Note that servers and caches may change or add headers during the request.

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

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(int, int) method.

Referer

Set by the HttpWebRequest.Referer property.

Transfer-Encoding

Set by the HttpWebRequest.TransferEncoding property (the HttpWebRequest.SendChunked property must be true).

User-Agent

Set by the HttpWebRequest.UserAgent property.

Note:

System.Net.HttpWebRequest is registered automatically. You do not need to call the WebRequest.RegisterPrefix(string, IWebRequestCreate) method to register System.Net.HttpWebRequest before using URIs beginning with http:// or https://.

The local computer or application config file may specify that a default proxy be used. If the HttpWebRequest.Proxy property is specified, then the proxy settings from the HttpWebRequest.Proxy property override the local computer or application config file and the System.Net.HttpWebRequest instance will use the proxy settings specified. If no proxy is specified in a config file and the HttpWebRequest.Proxy property is unspecified, the System.Net.HttpWebRequest class uses the proxy settings inherited from Internet Explorer on the local computer. If there are no proxy settings in Internet Explorer, the request is sent directly to the server.

The System.Net.HttpWebRequest class parses a proxy bypass list with wildcard characters inherited from Internet Explorer differently than the bypass list is parsed directly by Internet Explorer. For example, the System.Net.HttpWebRequest class will parse a bypass list of "nt*" from Internet Explorer as a regular expression of "nt.$". This differs from the native behavior of Internet Explorer. So a URL of "http://intxxxxx" would bypass the proxy using the System.Net.HttpWebRequest class, but would not bypass the proxy using Internet Explorer.

Note:

The Framework caches SSL sessions as they are created and attempts to reuse a cached session for a new request, if possible. When attempting to reuse an SSL session, the Framework uses the first element of HttpWebRequest.ClientCertificates (if there is one), or tries to reuse an anonymous sessions if HttpWebRequest.ClientCertificates is empty.

Note:

For security reasons, cookies are disabled by default. If you want to use cookies, use the HttpWebRequest.CookieContainer property to enable cookies.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.x.x, 1.0.5000.0, 2.0.0.0, 4.0.0.0