Export (0) Print
Expand All

HttpWebRequest Class

Provides an HTTP-specific implementation of the WebRequest class.

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

[SerializableAttribute] 
public class HttpWebRequest : WebRequest, ISerializable
/** @attribute SerializableAttribute() */ 
public class HttpWebRequest extends WebRequest implements ISerializable
SerializableAttribute 
public class HttpWebRequest extends WebRequest implements ISerializable
Not applicable.

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 System.Net.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://.

If the local computer configuration specifies that a proxy be used, or if the request specifies a proxy, the request is sent using the proxy. If no proxy is specified, the request is sent to the server.

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.

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 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 HttpWebRequest for the URI http://www.contoso.com/.


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


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


var myReq : HttpWebRequest = 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

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

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft