예제 #1
0
        private void ReadCallBack(IAsyncResult asyncResult)
        {
            Encoding encoding = Encoding.UTF8;

            // Get the RequestState object from AsyncResult.
            WebRequestState rs = (WebRequestState)asyncResult.AsyncState;

            // Retrieve the ResponseStream that was set in RespCallback.
            Stream responseStream = rs.ResponseStreamAsync;


            // Read rs.BufferRead to verify that it contains data.
            int read = responseStream.EndRead(asyncResult);

            if (read > 0)
            {
                // Convert byte stream to Char array and then to String.
                // len contains the number of characters converted to Unicode.
                ResponseStream.Write(rs.BufferRead, 0, read);

                // Continue reading data until
                // responseStream.EndRead returns –1.
                IAsyncResult ar = responseStream.BeginRead(
                    rs.BufferRead, 0, BUFFER_SIZE,
                    new AsyncCallback(ReadCallBack), rs);
            }
            else
            {
                if (ResponseStream.Length > 0)
                {
                    ResponseStream.Position = 0;
                    Complete = true;
                    Success  = true;

                    if (CallbackSuccess != null)
                    {
                        CallbackSuccess(this);
                    }
                }
                else
                {
                    Complete = true;
                    Success  = false;
                    if (CallbackFail != null)
                    {
                        CallbackFail(this);
                    }
                }
                // Close down the response stream.
                responseStream.Close();
                // Set the ManualResetEvent so the main thread can exit.

                allDone.Set();
            }
            return;
        }
예제 #2
0
        private void ReadCallBack(IAsyncResult asyncResult)
        {
            // Get the RequestState object from AsyncResult.
            WebRequestState rs = (WebRequestState)asyncResult.AsyncState;

            // Retrieve the ResponseStream that was set in RespCallback.
            Stream responseStream = rs.ResponseStream;

            // Read rs.BufferRead to verify that it contains data.
            int read = responseStream.EndRead(asyncResult);

            if (read > 0)
            {
                // Prepare a Char array buffer for converting to Unicode.
                Char[] charBuffer = new Char[BUFFER_SIZE];

                // Convert byte stream to Char array and then to String.
                // len contains the number of characters converted to Unicode.
                int len =
                    rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);

                String str = new String(charBuffer, 0, len);

                // Append the recently read data to the RequestData stringbuilder
                // object contained in RequestState.
                rs.RequestData.Append(
                    Encoding.UTF8.GetString(rs.BufferRead, 0, read));

                // Continue reading data until
                // responseStream.EndRead returns –1.
                IAsyncResult ar = responseStream.BeginRead(
                    rs.BufferRead, 0, BUFFER_SIZE,
                    new AsyncCallback(ReadCallBack), rs);
            }
            else
            {
                if (rs.RequestData.Length > 0)
                {
                    //  Display data to the console.
                    HtmlStringbuilder = rs.RequestData;

                    if (Callback != null)
                    {
                        Callback(this);
                    }
                }
                // Close down the response stream.
                responseStream.Close();
                // Set the ManualResetEvent so the main thread can exit.

                allDone.Set();
            }
            return;
        }
예제 #3
0
        public ManualResetEvent GetAsync()
        {
            // Create the state object.
            WebRequestState rs = new WebRequestState(this);

            // Put the request into the state object so it can be passed around.
            rs.Request = Request;

            // Issue the async request.

            IAsyncResult r = (IAsyncResult)Request.BeginGetResponse(
                new AsyncCallback(RespCallback), rs);

            return(allDone);
        }
예제 #4
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);
        }
예제 #5
0
        private void RespCallback(IAsyncResult ar)
        {
            // Get the RequestState object from the async result.
            WebRequestState rs = (WebRequestState)ar.AsyncState;

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

            // Call EndGetResponse, which produces the WebResponse object
            //  that came from the request issued above.
            WebResponse resp = req.EndGetResponse(ar);

            //  Start reading data from the response stream.
            Stream ResponseStream = resp.GetResponseStream();

            // Store the response stream in RequestState to read
            // the stream asynchronously.
            rs.ResponseStream = ResponseStream;

            //  Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead
            IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0,
                                                            BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
        }
예제 #6
0
        /// <summary>
        /// Begin the async request
        /// </summary>
        /// <returns></returns>
        public ManualResetEvent GetAsync()
        {
            Started = DateTime.Now;

            // Create the state object.
            WebRequestState rs = new WebRequestState(this);
            
            // Put the request into the state object so it can be passed around.
            rs.Request = Request;

            // Issue the async request.

            IAsyncResult r = (IAsyncResult)Request.BeginGetResponse(
               new AsyncCallback(RespCallback), rs);
            return allDone;
        }