Provides an HTTP-specific implementation of the WebRequest class.
Namespace:
System.Net
Assembly:
System.Net (in System.Net.dll)
Visual Basic (Declaration)
Public MustInherit Class HttpWebRequest _
Inherits WebRequest
Dim instance As HttpWebRequest
public abstract class HttpWebRequest : WebRequest
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 | 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 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 version 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).
try
{
System.Uri uri = new Uri("http://www.contoso.com");
// Create a HttpWebrequest object to the desired URL.
HttpWebRequest myHttpWebRequest1= (HttpWebRequest)WebRequest.Create(uri);
// Create an instance of the RequestState and assign the previous myHttpWebRequest1
// object to it's request field.
RequestState myRequestState = new RequestState();
myRequestState.request = myHttpWebRequest1;
// Start the asynchronous request.
IAsyncResult result=
(IAsyncResult) myHttpWebRequest1.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);
allDone.WaitOne();
// Release the HttpWebResponse resource.
myRequestState.response.Close();
}
catch(WebException e)
{
outputBlock.Text += "\nException raised!\n";
outputBlock.Text += "Message: ";
outputBlock.Text += e.Message;
outputBlock.Text += "\nStatus: ";
outputBlock.Text += e.Status;
outputBlock.Text += "\n";
}
catch(Exception e)
{
outputBlock.Text += "\nException raised!\n";
outputBlock.Text += "\nMessage: ";
outputBlock.Text += e.Message;
outputBlock.Text += "\n";
}
}
private static void RespCallback(IAsyncResult asynchronousResult)
{
try
{
// State of request is asynchronous.
RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
HttpWebRequest myHttpWebRequest2=myRequestState.request;
myRequestState.response = (HttpWebResponse) myHttpWebRequest2.EndGetResponse(asynchronousResult);
// Read the response into a Stream object.
Stream responseStream = myRequestState.response.GetResponseStream();
myRequestState.streamResponse=responseStream;
// Begin the Reading of the contents of the HTML page and print it to the console.
IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
}
catch(WebException e)
{
// Need to handle the exception
}
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
try
{
RequestState myRequestState = (RequestState)asyncResult.AsyncState;
Stream responseStream = myRequestState.streamResponse;
int read = responseStream.EndRead( asyncResult );
// Read the HTML page and then do something with it
if (read > 0)
{
myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read));
IAsyncResult asynchronousResult = responseStream.BeginRead( myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
}
else
{
if(myRequestState.requestData.Length>1)
{
string stringContent;
stringContent = myRequestState.requestData.ToString();
// do something with the response stream here
}
responseStream.Close();
allDone.Set();
}
}
catch(WebException e)
{
// Need to handle the exception
}
}
System..::.Object
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.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference
Other Resources