Provides an HTTP-specific implementation of the WebRequest class.
Assembly: System (in System.dll)
The 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 constructor. Use the WebRequest.Create method to initialize new objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, Create returns an object.
The GetResponse method makes a synchronous request to the resource specified in the RequestUri property and returns an HttpWebResponse that contains the response. You can make an asynchronous request to the resource using the BeginGetResponse and EndGetResponse methods.
When you want to send data to the resource, the GetRequestStream method returns a Stream object to use to send data. The BeginGetRequestStream and EndGetRequestStream methods provide asynchronous access to the send data stream.
For client authentication with , the client certificate must be installed in the My certificate store of the current user.
The class throws a WebException when errors occur while accessing a resource. The WebException.Status property contains a WebExceptionStatus value that indicates the source of the error. When WebException.Status is WebExceptionStatus.ProtocolError, the Response property contains the HttpWebResponse received from the resource.
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 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.
Set by the Accept property.
Set by the ContentLength property.
Set by the ContentType property.
Set by the Expect property.
Set by the system to current date.
Set by the system to current host information.
Set by the IfModifiedSince property.
Set by the AddRange method.
Set by the Referer property.
Set by the UserAgent property.
is registered automatically. You do not need to call the RegisterPrefix method to register 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 Proxy property is specified, then the proxy settings from the Proxy property override the local computer or application config file and the instance will use the proxy settings specified. If no proxy is specified in a config file and the Proxy property is unspecified, the 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 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 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 class, but would not bypass the proxy using Internet Explorer.
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 ClientCertificates (if there is one), or tries to reuse an anonymous sessions if ClientCertificates is empty.
A number of elements can have an impact on performance when using the class. These include the following:
The ServicePointManager is an object that provides connection management for HTTP connections. The ServicePointManager is a static class that creates maintains and deletes instances of the ServicePoint class. Each ServicePoint instance handles or manages connections to Internet resources based on the host Uri. After the first connection made to a host Uri, each subsequent request will use the information provided by the ServicePoint instance.
ServicePoint mySP = ServicePointManager.FindServicePoint(myUri);
The DefaultConnectionLimit property allows an application to specify how many concurrent connections should be allowed to any given resource. An application using a instance is able to specify the maximum number of concurrent persistent connections that it will have to a server. By default this is property is set to 2. So when the ServicePointManager creates a ServicePoint instance, it will assign the ConnectionLimit property to the value of the DefaultConnectionLimit property that is set on the ServicePointManager class.
The best value for the DefaultConnectionLimit property depends on the application workload and proxy settings. A general recommendation when only connecting to a few hosts or connecting through a proxy is to use a value that is 12 times the number of CPUs on the local computer. So for a computer with 4 CPUs using a proxy, the recommended value would be 48.
ServicePointManager.DefaultConnectionLimit = 48;
An application should increase the value for the DefaultConnectionLimit property under the following conditions:
If the application uses a proxy.
If the application primarily sends data and connects to a small number of servers.
If the application connects to systems that have high latency.
An application should decrease the value for the DefaultConnectionLimit property under the following conditions:
If the application is memory or resource constrained.
Another option that can have an impact on performance is the use of the UseNagleAlgorithm property. When this property is set to true, TCP/IP will try to use the TCP Nagle algorithm for HTTP connections. The Nagle algorithm aggregates data when sending TCP packets. It accumulates sequences of small messages into larger TCP packets before the data is sent over the network. Using the Nagle algorithm can optimize the use of network resources, although in some situations performance can also be degraded. Generally for constant high-volume throughput, a performance improvement is realized using the Nagle algorithm. But for smaller throughput applications, degradation in performance may be seen.
An application doesn't normally need to change the default value for the UseNagleAlgorithm property which is set to true. However, if an application is using low-latency connections, it may help to set this property to false.
ServicePointManager.UseNagleAlgorithm = false;
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: An HTTP POST request over Secure Sockets Layer (SSL) will fail with a SocketException if the request goes through a proxy and all of the following conditions are also met:
Preauthentication is disabled on the server.
The server requires Basic, Digest, Negotiate, or Windows NT Challenge/Response (NTLM) authentication.
KeepAlive is set to false.
X.509 client authentication is not used.
The following code example creates an for the URI http://www.contoso.com/.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.