Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
System.Net
Collapse All/Expand All Collapse All
.NET Framework Class Library
HttpWebRequest Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Provides an HTTP-specific implementation of the WebRequest class.

System..::.Object
  System..::.MarshalByRefObject
    System.Net..::.WebRequest
      System.Net..::.HttpWebRequest

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic
<SerializableAttribute> _
Public Class HttpWebRequest _
	Inherits WebRequest _
	Implements ISerializable
C#
[SerializableAttribute]
public class HttpWebRequest : WebRequest, 
	ISerializable
Visual C++
[SerializableAttribute]
public ref class HttpWebRequest : public WebRequest, 
	ISerializable
F#
[<SerializableAttribute>]
type HttpWebRequest =  
    class
        inherit WebRequest
        interface ISerializable
    end

The HttpWebRequest type exposes the following members.

  NameDescription
Public methodHttpWebRequest()()() Obsolete.
Protected methodHttpWebRequest(SerializationInfo, StreamingContext) Obsolete. Initializes a new instance of the HttpWebRequest class from the specified instances of the SerializationInfo and StreamingContext classes.
Top
  NameDescription
Public propertySupported by Portable Class LibraryAcceptGets or sets the value of the Accept HTTP header.
Public propertyAddressGets the Uniform Resource Identifier (URI) of the Internet resource that actually responds to the request.
Public propertyAllowAutoRedirectGets or sets a value that indicates whether the request should follow redirection responses.
Public propertyAllowReadStreamBuffering
Public propertyAllowWriteStreamBufferingGets or sets a value that indicates whether to buffer the data sent to the Internet resource.
Public propertyAuthenticationLevelGets or sets values indicating the level of authentication and impersonation used for this request. (Inherited from WebRequest.)
Public propertyAutomaticDecompressionGets or sets the type of decompression that is used.
Public propertyCachePolicyGets or sets the cache policy for this request. (Inherited from WebRequest.)
Public propertyClientCertificatesGets or sets the collection of security certificates that are associated with this request.
Public propertyConnectionGets or sets the value of the Connection HTTP header.
Public propertyConnectionGroupNameGets or sets the name of the connection group for the request. (Overrides WebRequest..::.ConnectionGroupName.)
Public propertyContentLengthGets or sets the Content-length HTTP header. (Overrides WebRequest..::.ContentLength.)
Public propertySupported by Portable Class LibraryContentTypeGets or sets the value of the Content-type HTTP header. (Overrides WebRequest..::.ContentType.)
Public propertyContinueDelegateGets or sets the delegate method called when an HTTP 100-continue response is received from the Internet resource.
Public propertyContinueTimeout
Public propertySupported by Portable Class LibraryCookieContainerGets or sets the cookies associated with the request.
Public propertyCreatorInstance Obsolete. (Inherited from WebRequest.)
Public propertyCredentialsGets or sets authentication information for the request. (Overrides WebRequest..::.Credentials.)
Public propertyDateGet or set the Date HTTP header value to use in an HTTP request.
Public propertyStatic memberDefaultCachePolicyGets or sets the default cache policy for this request.
Public propertyStatic memberDefaultMaximumErrorResponseLengthGets or sets the default maximum length of an HTTP error response.
Public propertyStatic memberDefaultMaximumResponseHeadersLengthGets or sets the default for the MaximumResponseHeadersLength property.
Public propertyExpectGets or sets the value of the Expect HTTP header.
Public propertySupported by Portable Class LibraryHaveResponseGets a value that indicates whether a response has been received from an Internet resource.
Public propertySupported by Portable Class LibraryHeadersSpecifies a collection of the name/value pairs that make up the HTTP headers. (Overrides WebRequest..::.Headers.)
Public propertyHostGet or set the Host header value to use in an HTTP request independent from the request URI.
Public propertyIfModifiedSinceGets or sets the value of the If-Modified-Since HTTP header.
Public propertyImpersonationLevelGets or sets the impersonation level for the current request. (Inherited from WebRequest.)
Public propertyKeepAliveGets or sets a value that indicates whether to make a persistent connection to the Internet resource.
Public propertyMaximumAutomaticRedirectionsGets or sets the maximum number of redirects that the request follows.
Public propertyMaximumResponseHeadersLengthGets or sets the maximum allowed length of the response headers.
Public propertyMediaTypeGets or sets the media type of the request.
Public propertySupported by Portable Class LibraryMethodGets or sets the method for the request. (Overrides WebRequest..::.Method.)
Public propertyPipelinedGets or sets a value that indicates whether to pipeline the request to the Internet resource.
Public propertyPreAuthenticateGets or sets a value that indicates whether to send an Authorization header with the request. (Overrides WebRequest..::.PreAuthenticate.)
Public propertyProtocolVersionGets or sets the version of HTTP to use for the request.
Public propertyProxyGets or sets proxy information for the request. (Overrides WebRequest..::.Proxy.)
Public propertyReadWriteTimeoutGets or sets a time-out in milliseconds when writing to or reading from a stream.
Public propertyRefererGets or sets the value of the Referer HTTP header.
Public propertySupported by Portable Class LibraryRequestUriGets the original Uniform Resource Identifier (URI) of the request. (Overrides WebRequest..::.RequestUri.)
Public propertySendChunkedGets or sets a value that indicates whether to send data in segments to the Internet resource.
Public propertyServerCertificateValidationCallback
Public propertyServicePointGets the service point to use for the request.
Public propertySupported by Portable Class LibrarySupportsCookieContainer
Public propertyTimeoutGets or sets the time-out value in milliseconds for the GetResponse and GetRequestStream methods. (Overrides WebRequest..::.Timeout.)
Public propertyTransferEncodingGets or sets the value of the Transfer-encoding HTTP header.
Public propertyUnsafeAuthenticatedConnectionSharingGets or sets a value that indicates whether to allow high-speed NTLM-authenticated connection sharing.
Public propertyUseDefaultCredentialsGets or sets a Boolean value that controls whether default credentials are sent with requests. (Overrides WebRequest..::.UseDefaultCredentials.)
Public propertyUserAgentGets or sets the value of the User-agent HTTP header.
Top
  NameDescription
Public methodSupported by Portable Class LibraryAbortCancels a request to an Internet resource. (Overrides WebRequest..::.Abort()()().)
Public methodAddRange(Int32)Adds a byte range header to a request for a specific range from the beginning or end of the requested data.
Public methodAddRange(Int64)Adds a byte range header to a request for a specific range from the beginning or end of the requested data.
Public methodAddRange(Int32, Int32)Adds a byte range header to the request for a specified range.
Public methodAddRange(Int64, Int64)Adds a byte range header to the request for a specified range.
Public methodAddRange(String, Int32)Adds a Range header to a request for a specific range from the beginning or end of the requested data.
Public methodAddRange(String, Int64)Adds a Range header to a request for a specific range from the beginning or end of the requested data.
Public methodAddRange(String, Int32, Int32)Adds a range header to a request for a specified range.
Public methodAddRange(String, Int64, Int64)Adds a range header to a request for a specified range.
Public methodSupported by Portable Class LibraryBeginGetRequestStreamBegins an asynchronous request for a Stream object to use to write data. (Overrides WebRequest..::.BeginGetRequestStream(AsyncCallback, Object).)
Public methodSupported by Portable Class LibraryBeginGetResponseBegins an asynchronous request to an Internet resource. (Overrides WebRequest..::.BeginGetResponse(AsyncCallback, Object).)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodSupported by Portable Class LibraryEndGetRequestStream(IAsyncResult)Ends an asynchronous request for a Stream object to use to write data. (Overrides WebRequest..::.EndGetRequestStream(IAsyncResult).)
Public methodEndGetRequestStream(IAsyncResult, TransportContext%)Ends an asynchronous request for a Stream object to use to write data and outputs the TransportContext associated with the stream.
Public methodSupported by Portable Class LibraryEndGetResponseEnds an asynchronous request to an Internet resource. (Overrides WebRequest..::.EndGetResponse(IAsyncResult).)
Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetObjectDataInfrastructure. Populates a SerializationInfo with the data required to serialize the target object. (Overrides WebRequest..::.GetObjectData(SerializationInfo, StreamingContext).)
Public methodGetRequestStream()()()Gets a Stream object to use to write request data. (Overrides WebRequest..::.GetRequestStream()()().)
Public methodGetRequestStream(TransportContext%)Gets a Stream object to use to write request data and outputs the TransportContext associated with the stream.
Public methodGetRequestStreamAsync (Inherited from WebRequest.)
Public methodGetResponseReturns a response from an Internet resource. (Overrides WebRequest..::.GetResponse()()().)
Public methodGetResponseAsync (Inherited from WebRequest.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodSupported by Portable Class LibraryMemberwiseClone()()()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Explicit interface implemetationPrivate methodISerializable..::.GetObjectDataInfrastructure. Populates a SerializationInfo with the data needed to serialize the target object.
Top

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 WebRequest..::.Create 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 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 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 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.

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 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.

Header

Set by

Accept

Set by the Accept property.

Connection

Set by the Connection property, KeepAlive property.

Content-Length

Set by the ContentLength property.

Content-Type

Set by the ContentType property.

Expect

Set by the 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 IfModifiedSince property.

Range

Set by the AddRange method.

Referer

Set by the Referer property.

Transfer-Encoding

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

User-Agent

Set by the UserAgent property.

NoteNote

HttpWebRequest is registered automatically. You do not need to call the RegisterPrefix 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 Proxy property is specified, then the proxy settings from the Proxy 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 Proxy 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.

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

NoteNote

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

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

Visual Basic

        Dim myReq As HttpWebRequest = _
            WebRequest.Create("http://www.contoso.com/")


C#

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


Visual C++
HttpWebRequest^ myReq = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "http://www.contoso.com/" ));

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
  • WebPermission 

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

Windows 8 Release Preview, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker