HttpWebRequest Class
 

Provides an HTTP-specific implementation of the WebRequest class.

Namespace:   System.Net
Assembly:  System (in System.dll)

SystemObject
   SystemMarshalByRefObject
     System.NetWebRequest
      System.NetHttpWebRequest

[SerializableAttribute]
public class HttpWebRequest : WebRequest, ISerializable
[SerializableAttribute]
public ref class HttpWebRequest : WebRequest, ISerializable
[<SerializableAttribute>]
type HttpWebRequest = 
    class
        inherit WebRequest
        interface ISerializable
    end
<SerializableAttribute>
Public Class HttpWebRequest
	Inherits WebRequest
	Implements ISerializable
NameDescription
System_CAPS_pubmethod HttpWebRequest

Obsolete. Initializes a new instance of the HttpWebRequest class.

System_CAPS_protmethod HttpWebRequest

Obsolete. Initializes a new instance of the HttpWebRequest class from the specified instances of the SerializationInfo and StreamingContext classes.

NameDescription
System_CAPS_pubproperty

Gets or sets the value of the Accept HTTP header.

System_CAPS_pubproperty

Gets the Uniform Resource Identifier (URI) of the Internet resource that actually responds to the request.

System_CAPS_pubproperty

Gets or sets a value that indicates whether the request should follow redirection responses.

System_CAPS_pubproperty

Gets or sets a value that indicates whether to buffer the received from the Internet resource.

System_CAPS_pubproperty

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

System_CAPS_pubproperty

Gets or sets values indicating the level of authentication and impersonation used for this request.(Inherited from WebRequest.)

System_CAPS_pubproperty

Gets or sets the type of decompression that is used.

System_CAPS_pubproperty

Gets or sets the cache policy for this request.(Inherited from WebRequest.)

System_CAPS_pubproperty

Gets or sets the collection of security certificates that are associated with this request.

System_CAPS_pubproperty

Gets or sets the value of the Connection HTTP header.

System_CAPS_pubproperty

Gets or sets the name of the connection group for the request.(Overrides .)

System_CAPS_pubproperty

Gets or sets the Content-length HTTP header.(Overrides .)

System_CAPS_pubproperty

Gets or sets the value of the Content-type HTTP header.(Overrides .)

System_CAPS_pubproperty

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

System_CAPS_pubproperty

Gets or sets a timeout, in milliseconds, to wait until the 100-Continue is received from the server.

System_CAPS_pubproperty

Gets or sets the cookies associated with the request.

System_CAPS_pubproperty

Obsolete. When overridden in a descendant class, gets the factory object derived from the IWebRequestCreate class used to create the WebRequest instantiated for making the request to the specified URI.(Inherited from WebRequest.)

System_CAPS_pubproperty

Gets or sets authentication information for the request.(Overrides .)

System_CAPS_pubproperty

Get or set the Date HTTP header value to use in an HTTP request.

System_CAPS_pubproperty System_CAPS_static

Gets or sets the default cache policy for this request.

System_CAPS_pubproperty System_CAPS_static

Gets or sets the default maximum length of an HTTP error response.

System_CAPS_pubproperty System_CAPS_static

Gets or sets the default for the property.

System_CAPS_pubproperty

Gets or sets the value of the Expect HTTP header.

System_CAPS_pubproperty

Gets a value that indicates whether a response has been received from an Internet resource.

System_CAPS_pubproperty

Specifies a collection of the name/value pairs that make up the HTTP headers.(Overrides .)

System_CAPS_pubproperty

Get or set the Host header value to use in an HTTP request independent from the request URI.

System_CAPS_pubproperty

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

System_CAPS_pubproperty

Gets or sets the impersonation level for the current request.(Inherited from WebRequest.)

System_CAPS_pubproperty

Gets or sets a value that indicates whether to make a persistent connection to the Internet resource.

System_CAPS_pubproperty

Gets or sets the maximum number of redirects that the request follows.

System_CAPS_pubproperty

Gets or sets the maximum allowed length of the response headers.

System_CAPS_pubproperty

Gets or sets the media type of the request.

System_CAPS_pubproperty

Gets or sets the method for the request.(Overrides .)

System_CAPS_pubproperty

Gets or sets a value that indicates whether to pipeline the request to the Internet resource.

System_CAPS_pubproperty

Gets or sets a value that indicates whether to send an Authorization header with the request.(Overrides .)

System_CAPS_pubproperty

Gets or sets the version of HTTP to use for the request.

System_CAPS_pubproperty

Gets or sets proxy information for the request.(Overrides .)

System_CAPS_pubproperty

Gets or sets a time-out in milliseconds when writing to or reading from a stream.

System_CAPS_pubproperty

Gets or sets the value of the Referer HTTP header.

System_CAPS_pubproperty

Gets the original Uniform Resource Identifier (URI) of the request.(Overrides .)

System_CAPS_pubproperty

Gets or sets a value that indicates whether to send data in segments to the Internet resource.

System_CAPS_pubproperty

Gets or sets a callback function to validate the server certificate.

System_CAPS_pubproperty

Gets the service point to use for the request.

System_CAPS_pubproperty

Gets a value that indicates whether the request provides support for a CookieContainer.

System_CAPS_pubproperty

Gets or sets the time-out value in milliseconds for the GetResponse and GetRequestStream methods.(Overrides .)

System_CAPS_pubproperty

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

System_CAPS_pubproperty

Gets or sets a value that indicates whether to allow high-speed NTLM-authenticated connection sharing.

System_CAPS_pubproperty

Gets or sets a Boolean value that controls whether default credentials are sent with requests.(Overrides .)

System_CAPS_pubproperty

Gets or sets the value of the User-agent HTTP header.

NameDescription
System_CAPS_pubmethod Abort

Cancels a request to an Internet resource.(Overrides WebRequestAbort.)

System_CAPS_pubmethod AddRange

Adds a byte range header to a request for a specific range from the beginning or end of the requested data.

System_CAPS_pubmethod AddRange

Adds a byte range header to the request for a specified range.

System_CAPS_pubmethod AddRange

Adds a byte range header to a request for a specific range from the beginning or end of the requested data.

System_CAPS_pubmethod AddRange

Adds a byte range header to the request for a specified range.

System_CAPS_pubmethod AddRange

Adds a Range header to a request for a specific range from the beginning or end of the requested data.

System_CAPS_pubmethod AddRange

Adds a range header to a request for a specified range.

System_CAPS_pubmethod AddRange

Adds a Range header to a request for a specific range from the beginning or end of the requested data.

System_CAPS_pubmethod AddRange

Adds a range header to a request for a specified range.

System_CAPS_pubmethod BeginGetRequestStream

Begins an asynchronous request for a Stream object to use to write data.(Overrides WebRequestBeginGetRequestStream.)

System_CAPS_pubmethod BeginGetResponse

Begins an asynchronous request to an Internet resource.(Overrides WebRequestBeginGetResponse.)

System_CAPS_pubmethod CreateObjRef

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethod EndGetRequestStream

Ends an asynchronous request for a Stream object to use to write data.(Overrides WebRequestEndGetRequestStream.)

System_CAPS_pubmethod EndGetRequestStream

Ends an asynchronous request for a Stream object to use to write data and outputs the TransportContext associated with the stream.

System_CAPS_pubmethod EndGetResponse

Ends an asynchronous request to an Internet resource.(Overrides WebRequestEndGetResponse.)

System_CAPS_pubmethod Equals

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethod Finalize

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethod GetHashCode

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethod GetLifetimeService

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethod GetObjectData

This API supports the product infrastructure and is not intended to be used directly from your code. Populates a SerializationInfo with the data required to serialize the target object.(Overrides WebRequestGetObjectData.)

System_CAPS_pubmethod GetRequestStream

Gets a Stream object to use to write request data.(Overrides WebRequestGetRequestStream.)

System_CAPS_pubmethod GetRequestStream

Gets a Stream object to use to write request data and outputs the TransportContext associated with the stream.

System_CAPS_pubmethod GetRequestStreamAsync

When overridden in a descendant class, returns a Stream for writing data to the Internet resource as an asynchronous operation.(Inherited from WebRequest.)

System_CAPS_pubmethod GetResponse

Returns a response from an Internet resource.(Overrides WebRequestGetResponse.)

System_CAPS_pubmethod GetResponseAsync

When overridden in a descendant class, returns a response to an Internet request as an asynchronous operation.(Inherited from WebRequest.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod InitializeLifetimeService

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterface System_CAPS_privmethod ISerializableGetObjectData

This API supports the product infrastructure and is not intended to be used directly from your code. Populates a SerializationInfo with the data needed to serialize the target object.

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 constructor. Use the WebRequestCreate method to initialize new HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, Create returns an HttpWebRequest object.

The GetResponse method makes a synchronous request to the resource specified in the property and returns an HttpWebResponse that contains the response object. The response data can be received by using the stream returned by GetResponseStream. If the response object or the response stream is closed, remaining data will be forfeited. The remaining data will be drained and the socket will be re-used for subsequent requests when closing the response object or stream if the following conditions hold: it's a keep-alive or pipelined request, only a small amount of data needs to be received, or the remaining data is received in a small time interval. If none of the mentioned conditions hold or the drain time is exceeded, the socket will be closed. For keep-alive or pipelined connections, we strongly recommend that the application reads the streams until EOF. This ensures that the socket will be re-used for subsequent requests resulting in better performance and less resources used.

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 HttpWebRequest, the client certificate must be installed in the My certificate store of the current user.

The HttpWebRequest class throws a WebException when errors occur while accessing a resource. The property contains a WebExceptionStatus value that indicates the source of the error. When is WebExceptionStatusProtocolError, the property contains the HttpWebResponse received from the 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 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.

Header

Set by

Accept

Set by the property.

Connection

Set by the property, property.

Content-Length

Set by the property.

Content-Type

Set by the property.

Expect

Set by the property.

Date

Set by the system to current date.

Host

Set by the system to current host information.

If-Modified-Since

Set by the property.

Range

Set by the AddRange method.

Referer

Set by the property.

Transfer-Encoding

Set by the property (the property must be true).

User-Agent

Set by the property.

System_CAPS_noteNote

HttpWebRequest is registered automatically. You do not need to call the RegisterPrefix method to register System.NetHttpWebRequest 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 property is specified, then the proxy settings from the property override the local computer or application config file and the HttpWebRequest instance will use the proxy settings specified. If no proxy is specified in a config file and the property is unspecified, the 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 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 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 HttpWebRequest class, but would not bypass the proxy using Internet Explorer.

System_CAPS_noteNote

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 (if there is one), or tries to reuse an anonymous sessions if is empty.

System_CAPS_noteNote

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

The .NET Framework 4.6 includes a new security feature that blocks insecure cipher and hashing algorithms for connections. Applications using TLS/SSL through APIs such as HttpClient, HttpWebRequest, FTPClient, SmtpClient, SslStream, etc. and targeting .NET Framework 4.6 get the more-secure behavior by default.

Developers may want to opt out of this behavior in order to maintain interoperability with their existing SSL3 services OR TLS w/ RC4 services. This article explains how to modify your code so that the new behavior is disabled.

The following code example creates an HttpWebRequest for the URI http://www.contoso.com/.


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

Dim myReq As HttpWebRequest = _
    WebRequest.Create("http://www.contoso.com/")
HttpWebRequest^ myReq = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "http://www.contoso.com/" ));
WebPermission

to access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top