Exemple #1
0
        private void RespCallback(IAsyncResult ar)
        {
            // Get the RequestState object from the async result.
            WebRequestState rs = (WebRequestState)ar.AsyncState;

            // Get the WebRequest from RequestState.
            IHttpWebRequest req = rs.Request;

            req.Timeout = Timeout;
            req.Headers["UserAgent"] = UserAgent;

            try
            {
                // Call EndGetResponse, which produces the WebResponse object
                //  that came from the request issued above.
                Response = (IHttpWebResponse)req.EndGetResponse(ar);
            }
            catch (WebException e)
            {
                Complete     = true;
                Success      = false;
                WebException = e;

                if (CallbackFail != null)
                {
                    CallbackFail(this);
                }
                allDone.Set();
                return;
            }

            ResponseEncoding = CsqWebRequest.GetEncoding(Response);

            //  Start reading data from the response stream.
            Stream responseStream = Response.GetResponseStream();

            // Store the response stream in RequestState to read
            // the stream asynchronously.
            rs.ResponseStreamAsync = responseStream;
            ResponseStream         = new MemoryStream();


            //  Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead
            IAsyncResult iarRead = responseStream.BeginRead(rs.BufferRead, 0,
                                                            BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
        }
Exemple #2
0
        private void ResponseCallback(IAsyncResult asyncResult, IHttpWebRequest httpWebRequest, Stream requestBody, AsyncCallback callback, object state)
        {
            IHttpWebResponse httpWebResponse = null;
            Exception        exception       = null;

            try
            {
                httpWebResponse = httpWebRequest.EndGetResponse(asyncResult);
            }
            catch (WebException ex)
            {
                var webException = new WebExceptionWrapper(ex);
                httpWebResponse = webException.GetResponse();
                exception       = webException;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    var args = new ResponseReceivedEventArgs(httpWebResponse, exception, state);
                    OnResponseReceived(args);
                    ReadResponseStream(httpWebRequest, httpWebResponse, exception, args.ResponseSaveStream, callback, state);
                }
                else
                {
                    if (callback != null)
                    {
                        callback(new HttpWebHelperResult(httpWebRequest, httpWebResponse, exception, null, false, false, null, state));
                    }
                }
            }
        }
        private void ResponseCallback(IAsyncResult asyncResult, IHttpWebRequest httpWebRequest, Stream requestBody, AsyncCallback callback, object state)
        {
            IHttpWebResponse httpWebResponse = null;
            Exception exception = null;

            try
            {
                httpWebResponse = httpWebRequest.EndGetResponse(asyncResult);
            }
            catch (WebException ex)
            {
                var webException = new WebExceptionWrapper(ex);
                httpWebResponse = webException.GetResponse();
                exception = webException;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    var args = new ResponseReceivedEventArgs(httpWebResponse, exception, state);
                    OnResponseReceived(args);
                    ReadResponseStream(httpWebRequest, httpWebResponse, exception, args.ResponseSaveStream, callback, state);
                }
                else
                {
                    if (callback != null)
                        callback(new HttpWebHelperResult(httpWebRequest, httpWebResponse, exception, null, false, false, null, state));
                }
            }
        }