HttpWebRequest Class
Provides an HTTP-specific implementation of the WebRequest class.
For a list of all members of this type, see HttpWebRequest Members.
System.Object
System.MarshalByRefObject
System.Net.WebRequest
System.Net.HttpWebRequest
[Visual Basic] <Serializable> Public Class HttpWebRequest Inherits WebRequest [C#] [Serializable] public class HttpWebRequest : WebRequest [C++] [Serializable] public __gc class HttpWebRequest : public WebRequest [JScript] public Serializable class HttpWebRequest extends WebRequest
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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 instances. If the scheme for the URI is http:// or https://, Create returns an HttpWebRequest instance.
The GetResponse method makes a synchronous request to the Internet resource specified in the RequestUri property and returns an HttpWebResponse instance containing the response. You can make an asynchronous request to the Internet resource using the BeginGetResponse and EndGetResponse methods.
When you want to send data to the Internet resource, the GetRequestStream method returns a Stream instance 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 an Internet resource. The WebException.Status property is one of the WebExceptionStatus values that indicates the source of the error. When WebException.Status is WebExceptionStatus.ProtocolError, the Response property contains the HttpWebResponse received from the Internet 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.
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. |
Note HttpWebRequest is registered automatically. You do not need to call RegisterPrefix to register System.Net.HttpWebRequest before using URIs beginning with http:// or https://.
Example
The following 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/"); [C++] HttpWebRequest* myReq = dynamic_cast<HttpWebRequest*>(WebRequest::Create(S"http://www.contoso.com/")); [JScript] var myReq : HttpWebRequest = HttpWebRequest(WebRequest.Create("http://www.contoso.com/"))
Requirements
Namespace: System.Net
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System (in System.dll)
.NET Framework Security:
- WebPermission to access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect.