System.Net.HttpWebRequest.BeginGetResponse Method

Begins an asynchronous request to an Internet resource.

Syntax

public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)

Parameters

callback
The AsyncCallback delegate
state
The state object for this request.

Returns

An IAsyncResult that references the asynchronous request for a response.

Exceptions

TypeReason
InvalidOperationException HttpWebRequest.BeginGetResponse(AsyncCallback, object) or HttpWebRequest.GetResponse was previously called on this instance.
System.Net.ProtocolViolationExceptionThe HttpWebRequest.ContentLength property of the current instance has not been set.
System.Net.WebException HttpWebRequest.Abort was previously called.

Remarks

The HttpWebRequest.BeginGetResponse(AsyncCallback, object) method starts an asynchronous request for a response from the Internet resource. The asynchronous callback method uses the HttpWebRequest.EndGetResponse(IAsyncResult) method to return the actual System.Net.WebResponse.

A System.Net.ProtocolViolationException is thrown in several cases when the properties set on the System.Net.HttpWebRequest class are conflicting. This exception occurs if an application sets the HttpWebRequest.ContentLength property and the HttpWebRequest.SendChunked property to true, and then sends an HTTP GET request. This exception occurs if an application tries to send chunked to a server that only supports HTTP 1.0 protocol, where this is not supported. This exception occurs if an application tries to send data without setting the HttpWebRequest.ContentLength property or the HttpWebRequest.SendChunked is false when buffering is disabled and on a keepalive connection (the HttpWebRequest.KeepAlive property is true).

If a System.Net.WebException is thrown, use the WebException.Response and WebException.Status properties of the exception to determine the response from the server.

The HttpWebRequest.BeginGetResponse(AsyncCallback, object) method requires some synchronous setup tasks to complete (DNS resolution, proxy detection, and TCP socket connection, for example) before this method becomes asynchronous. As a result, this method should never be called on a user interface (UI) thread because it might take considerable time (up to several minutes depending on network settings) to complete the initial synchronous setup tasks before an exception for an error is thrown or the method succeeds.

To learn more about the thread pool, see [<topic://cpconthreadpooling>].

Note:

Your application cannot mix synchronous and asynchronous methods for a particular request. If you call the HttpWebRequest.BeginGetRequestStream(AsyncCallback, object) method, you must use the HttpWebRequest.BeginGetResponse(AsyncCallback, object) method to retrieve the response.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0