/// <devdoc> /// <para> /// Starts async request processing including async retrieval of the request stream and response. /// Derived classes can use BeginSend /// to help implement their own higher level async methods like BeginInvoke. Derived /// classes can add custom behavior by overriding GetWebRequest, GetWebResponse, /// InitializeAsyncRequest and WriteAsyncRequest methods. /// </para> /// </devdoc> internal IAsyncResult BeginSend(Uri requestUri, WebClientAsyncResult asyncResult, bool callWriteAsyncRequest) { if (readResponseAsyncCallback == null) { lock (InternalSyncObject) { if (readResponseAsyncCallback == null) { getRequestStreamAsyncCallback = new AsyncCallback(GetRequestStreamAsyncCallback); getResponseAsyncCallback = new AsyncCallback(GetResponseAsyncCallback); readResponseAsyncCallback = new AsyncCallback(ReadResponseAsyncCallback); } } } Debug.Assert(asyncResult.Request == null, "calling GetWebRequest twice for the same WebClientAsyncResult"); WebRequest request = GetWebRequest(requestUri); asyncResult.Request = request; InitializeAsyncRequest(request, asyncResult.InternalAsyncState); if (callWriteAsyncRequest) { request.BeginGetRequestStream(getRequestStreamAsyncCallback, asyncResult); } else { request.BeginGetResponse(getResponseAsyncCallback, asyncResult); } if (!asyncResult.IsCompleted) { asyncResult.CombineCompletedSynchronously(false); } return(asyncResult); }
internal IAsyncResult BeginSend(System.Uri requestUri, WebClientAsyncResult asyncResult, bool callWriteAsyncRequest) { if (readResponseAsyncCallback == null) { lock (InternalSyncObject) { if (readResponseAsyncCallback == null) { getRequestStreamAsyncCallback = new AsyncCallback(WebClientProtocol.GetRequestStreamAsyncCallback); getResponseAsyncCallback = new AsyncCallback(WebClientProtocol.GetResponseAsyncCallback); readResponseAsyncCallback = new AsyncCallback(WebClientProtocol.ReadResponseAsyncCallback); } } } WebRequest webRequest = this.GetWebRequest(requestUri); asyncResult.Request = webRequest; this.InitializeAsyncRequest(webRequest, asyncResult.InternalAsyncState); if (callWriteAsyncRequest) { webRequest.BeginGetRequestStream(getRequestStreamAsyncCallback, asyncResult); } else { webRequest.BeginGetResponse(getResponseAsyncCallback, asyncResult); } if (!asyncResult.IsCompleted) { asyncResult.CombineCompletedSynchronously(false); } return(asyncResult); }
/// <devdoc> /// <para> /// Starts async request processing including async retrieval of the request stream and response. /// Derived classes can use BeginSend /// to help implement their own higher level async methods like BeginInvoke. Derived /// classes can add custom behavior by overriding GetWebRequest, GetWebResponse, /// InitializeAsyncRequest and WriteAsyncRequest methods. /// </para> /// </devdoc> internal IAsyncResult BeginSend(Uri requestUri, object internalAsyncState, AsyncCallback outerCallback, object outerAsyncState, bool callWriteAsyncRequest) { if (readResponseAsyncCallback == null) { lock (typeof(WebClientProtocol)) { if (readResponseAsyncCallback == null) { getRequestStreamAsyncCallback = new AsyncCallback(GetRequestStreamAsyncCallback); getResponseAsyncCallback = new AsyncCallback(GetResponseAsyncCallback); readResponseAsyncCallback = new AsyncCallback(ReadResponseAsyncCallback); } } } WebRequest request = GetWebRequest(requestUri); WebClientAsyncResult client = new WebClientAsyncResult(this, internalAsyncState, request, outerCallback, outerAsyncState, 0); InitializeAsyncRequest(request, internalAsyncState); if (callWriteAsyncRequest) { request.BeginGetRequestStream(getRequestStreamAsyncCallback, client); } else { request.BeginGetResponse(getResponseAsyncCallback, client); } if (!client.IsCompleted) { client.CombineCompletedSynchronously(false); } return(client); }
static private void GetResponseAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult client = (WebClientAsyncResult)asyncResult.AsyncState; client.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); try { client.Response = client.ClientProtocol.GetWebResponse(client.Request, asyncResult); } catch (Exception e) { ProcessAsyncException(client, e); if (client.Response == null) { return; } } ReadAsyncResponse(client); }
static private void GetRequestStreamAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult client = (WebClientAsyncResult)asyncResult.AsyncState; client.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); try { Stream requestStream = client.Request.EndGetRequestStream(asyncResult); try { client.ClientProtocol.AsyncBufferedSerialize(client.Request, requestStream, client.InternalAsyncState); } finally { requestStream.Close(); } client.Request.BeginGetResponse(getResponseAsyncCallback, client); } catch (Exception e) { ProcessAsyncException(client, e); } }
static private void ReadResponseAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult client = (WebClientAsyncResult)asyncResult.AsyncState; client.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); if (asyncResult.CompletedSynchronously) { return; } try { bool complete = ProcessAsyncResponseStreamResult(client, asyncResult); if (!complete) { ReadAsyncResponseStream(client); } } catch (Exception e) { ProcessAsyncException(client, e); } }
static private void GetResponseAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult client = (WebClientAsyncResult)asyncResult.AsyncState; client.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); try { client.Response = client.ClientProtocol.GetWebResponse(client.Request, asyncResult); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } ProcessAsyncException(client, e, "GetResponseAsyncCallback"); if (client.Response == null) { return; } } ReadAsyncResponse(client); }
static private void GetRequestStreamAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult client = (WebClientAsyncResult)asyncResult.AsyncState; client.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); bool processingRequest = true; try { Stream requestStream = client.Request.EndGetRequestStream(asyncResult); processingRequest = false; try { client.ClientProtocol.AsyncBufferedSerialize(client.Request, requestStream, client.InternalAsyncState); } finally { requestStream.Close(); } client.Request.BeginGetResponse(getResponseAsyncCallback, client); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } ProcessAsyncException(client, e, "GetRequestStreamAsyncCallback"); if (processingRequest) { WebException we = e as WebException; if (we != null && we.Response != null) { // ProcessAsyncExcption doesn't call client.Complete() if there's a response, // because it expects us to read the response. However, in certain cases // (e.g. 502 errors), the exception thrown from Request can have a response. // We don't process it, so call Complete() now. client.Complete(e); } } } }
private static void GetRequestStreamAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult asyncState = (WebClientAsyncResult)asyncResult.AsyncState; asyncState.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); bool flag = true; try { Stream requestStream = asyncState.Request.EndGetRequestStream(asyncResult); flag = false; try { asyncState.ClientProtocol.AsyncBufferedSerialize(asyncState.Request, requestStream, asyncState.InternalAsyncState); } finally { requestStream.Close(); } asyncState.Request.BeginGetResponse(getResponseAsyncCallback, asyncState); } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } ProcessAsyncException(asyncState, exception, "GetRequestStreamAsyncCallback"); if (flag) { WebException exception2 = exception as WebException; if ((exception2 != null) && (exception2.Response != null)) { asyncState.Complete(exception); } } } }
private static void GetResponseAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult asyncState = (WebClientAsyncResult)asyncResult.AsyncState; asyncState.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); try { asyncState.Response = asyncState.ClientProtocol.GetWebResponse(asyncState.Request, asyncResult); } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } ProcessAsyncException(asyncState, exception, "GetResponseAsyncCallback"); if (asyncState.Response == null) { return; } } ReadAsyncResponse(asyncState); }
static private void ReadResponseAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult client = (WebClientAsyncResult)asyncResult.AsyncState; client.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); if (asyncResult.CompletedSynchronously) { return; } try { bool complete = ProcessAsyncResponseStreamResult(client, asyncResult); if (!complete) { ReadAsyncResponseStream(client); } } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } ProcessAsyncException(client, e, "ReadResponseAsyncCallback"); } }
private static void ReadResponseAsyncCallback(IAsyncResult asyncResult) { WebClientAsyncResult asyncState = (WebClientAsyncResult)asyncResult.AsyncState; asyncState.CombineCompletedSynchronously(asyncResult.CompletedSynchronously); if (!asyncResult.CompletedSynchronously) { try { if (!ProcessAsyncResponseStreamResult(asyncState, asyncResult)) { ReadAsyncResponseStream(asyncState); } } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } ProcessAsyncException(asyncState, exception, "ReadResponseAsyncCallback"); } } }
/// <devdoc> /// <para> /// Starts async request processing including async retrieval of the request stream and response. /// Derived classes can use BeginSend /// to help implement their own higher level async methods like BeginInvoke. Derived /// classes can add custom behavior by overriding GetWebRequest, GetWebResponse, /// InitializeAsyncRequest and WriteAsyncRequest methods. /// </para> /// </devdoc> internal IAsyncResult BeginSend(Uri requestUri, WebClientAsyncResult asyncResult, bool callWriteAsyncRequest) { if (readResponseAsyncCallback == null) { lock (InternalSyncObject) { if (readResponseAsyncCallback == null) { getRequestStreamAsyncCallback = new AsyncCallback(GetRequestStreamAsyncCallback); getResponseAsyncCallback = new AsyncCallback(GetResponseAsyncCallback); readResponseAsyncCallback = new AsyncCallback(ReadResponseAsyncCallback); } } } Debug.Assert(asyncResult.Request == null, "calling GetWebRequest twice for the same WebClientAsyncResult"); WebRequest request = GetWebRequest(requestUri); asyncResult.Request = request; InitializeAsyncRequest(request, asyncResult.InternalAsyncState); if (callWriteAsyncRequest) request.BeginGetRequestStream(getRequestStreamAsyncCallback, asyncResult); else request.BeginGetResponse(getResponseAsyncCallback, asyncResult); if (!asyncResult.IsCompleted) asyncResult.CombineCompletedSynchronously(false); return asyncResult; }
internal IAsyncResult BeginSend(System.Uri requestUri, WebClientAsyncResult asyncResult, bool callWriteAsyncRequest) { if (readResponseAsyncCallback == null) { lock (InternalSyncObject) { if (readResponseAsyncCallback == null) { getRequestStreamAsyncCallback = new AsyncCallback(WebClientProtocol.GetRequestStreamAsyncCallback); getResponseAsyncCallback = new AsyncCallback(WebClientProtocol.GetResponseAsyncCallback); readResponseAsyncCallback = new AsyncCallback(WebClientProtocol.ReadResponseAsyncCallback); } } } WebRequest webRequest = this.GetWebRequest(requestUri); asyncResult.Request = webRequest; this.InitializeAsyncRequest(webRequest, asyncResult.InternalAsyncState); if (callWriteAsyncRequest) { webRequest.BeginGetRequestStream(getRequestStreamAsyncCallback, asyncResult); } else { webRequest.BeginGetResponse(getResponseAsyncCallback, asyncResult); } if (!asyncResult.IsCompleted) { asyncResult.CombineCompletedSynchronously(false); } return asyncResult; }