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://, WebRequest..::.Create returns an HttpWebRequest object.
You can make an asynchronous request to the resource using the BeginGetResponse and EndGetResponse methods.
The BeginGetRequestStream and EndGetRequestStream methods provide asynchronous access to the send data stream.
The HttpWebRequest class throws a WebException when an error occurs while accessing a resource. The WebException..::.Status property contains a WebExceptionStatus value that indicates the source of the error.
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 web browser that hosts the Silverlight application. |
Connection | Set by the web browser that hosts the Silverlight application. |
Content-Length | On Silverlight 4, set by the ContentLength property. On the browser HTTP stack, the ContentLength property is not required to be set because the Silverlight runtime will automatically populate the header with the correct value based on the buffered request body. If a Silverlight application on either the browser or client HTTP stack sets this property to a value that does not match the size of the provided request body, a ProtocolViolationException is thrown when the request is sent. On Silverlight 3, set by the web browser that hosts the Silverlight application. |
Content-Type | Set by the ContentType property. This header cannot be set on a GET method. If this header is set, a ProtocolViolationException is thrown. |
Expect | Set by the web browser that hosts the Silverlight application. |
If-Modified-Since | Set by the web browser that hosts the Silverlight application. |
Referer | Set by the web browser that hosts the Silverlight application. |
Transfer-Encoding | Set by the web browser that hosts the Silverlight application. |
User-Agent | Set by the web browser that hosts the Silverlight application. |
The Mozilla FireFox browser does not currently set the Referer header on HTTP GET requests. The Internet Explorer and Safari web browsers do properly set the Referer header on HTTP GET requests. As a result, a Silverlight application hosted in Firefox can't support the Referer header being set on any GET requests applicable to browser HTTP handling for BrowserHttp. The Referer header is properly set on HTTP POST requests by Firefox, Internet Explorer, and Safari applicable to browser HTTP handling for BrowserHttp.
The Referer header is properly set on all HTTP requests applicable to client HTTP handling for ClientHttp regardless of the web browser.
The Item property can be used to set custom headers.
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.
Note: |
|---|
For security reasons, cookies are disabled by default. |
Silverlight 2 and later includes support for cross-domain connectivity which allows an application to access resources from locations other than the site of origin. This is an important feature for enabling Silverlight applications to consume existing services on the web.
The security policy system in the Silverlight runtime requires that a policy file be downloaded from a network resource before a network connection is allowed access to that resource. This security policy system affects cross-domain network access for WebClient and HTTP classes in the System.Net namespace. Network connections for WebClient and HTTP classes to the site or host of origin do not require a security policy.
For security reasons, the Silverlight runtime restricts access to certain classes of URLs from the WebClient and HTTP classes in the System.Net namespace. There are similar access restrictions applied by the Silverlight runtime to other classes including the Image and MediaElement classes in the System.Windows.Controls namespace. The Silverlight runtime also applies access restrictions to XAML source files and font files based on the class of URL.
The connections affected are access to cross-zone, cross-domain, and cross-scheme URLs. These restrictions are designed to prevent networking threats (for example, threats based on a Silverlight application run from an internet server getting access to resources on a local intranet server). For more information, see Network Security Access Restrictions in Silverlight and URL Access Restrictions in Silverlight.
For security reasons, the Silverlight runtime restricts the HttpWebRequest class from sending specific headers to a cross-domain site unless the header is allowed by the security policy applicable to the target cross-domain site. This restriction applies to resources from locations other than the site of origin. The Authorization header can be set using the Headers property. However to set the credentials properly, the cross-domain policy applicable to the target must have the http-request-headers set to allow the Authorization header to be sent.
Version Notes
Silverlight for Windows Phone
This type is not abstract in Silverlight for Windows Phone.
Six active Web service connections are allowed simultaneously. Additional requests are paused until a connection is available.