Exemple #1
0
        void BeginGetResponseCallback(IAsyncResult asyncResult)
        {
            WebResponse webResponse             = null;
            Stream      responseStream          = null;
            HttpWebRequestAsyncState asyncState = null;

            try
            {
                asyncState     = (HttpWebRequestAsyncState)asyncResult.AsyncState;
                webResponse    = asyncState.HttpWebRequest.EndGetResponse(asyncResult);
                responseStream = webResponse.GetResponseStream();
                var webRequestCallbackState = new HttpWebRequestCallbackState(responseStream, asyncState.State);
                asyncState.ResponseCallback(webRequestCallbackState);
                responseStream.Close();
                responseStream = null;
                webResponse.Close();
                webResponse = null;
            }
            catch (Exception ex)
            {
                //SharpLog.Error("BeginGetResponseCallback", ex.ToString());
                if (asyncState != null)
                {
                    asyncState.ResponseCallback(new HttpWebRequestCallbackState(ex));
                }
                else
                {
                    throw new Exception(ex.ToString());
                }
            }
            finally
            {
                if (responseStream != null)
                {
                    responseStream.Close();
                }
                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
        }
Exemple #2
0
        void BeginGetRequestStreamCallback(IAsyncResult asyncResult)
        {
            Stream requestStream = null;
            HttpWebRequestAsyncState asyncState = null;

            try
            {
                asyncState    = (HttpWebRequestAsyncState)asyncResult.AsyncState;
                requestStream = asyncState.HttpWebRequest.EndGetRequestStream(asyncResult);
                requestStream.Write(asyncState.RequestBytes, 0, asyncState.RequestBytes.Length);
                requestStream.Close();
                asyncState.HttpWebRequest.BeginGetResponse(BeginGetResponseCallback,
                                                           new HttpWebRequestAsyncState
                {
                    HttpWebRequest   = asyncState.HttpWebRequest,
                    ResponseCallback = asyncState.ResponseCallback,
                    State            = asyncState.State
                });
            }
            catch (Exception ex)
            {
                if (asyncState != null)
                {
                    asyncState.ResponseCallback(new HttpWebRequestCallbackState(ex));
                }
                else
                {
                    throw new Exception(ex.ToString());
                }
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
            }
        }