示例#1
0
        internal static void ResponseCallback(IAsyncResult result)
        {
            StateContainer states = (StateContainer)result.AsyncState;

            try
            {
                WebResponse response = states.Request.EndGetResponse(result);

                using (Stream stream = response.GetResponseStream())
                {
                    const int BufferLength = 8096;
                    byte[]    buffer       = new byte[BufferLength];
                    string    responseString;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        int bytesRead;

                        while ((bytesRead = stream.Read(buffer, 0, BufferLength)) > 0)
                        {
                            ms.Write(buffer, 0, bytesRead);
                        }

                        ms.Flush();

                        responseString = Encoding.UTF8.GetString(ms.ToArray());
                    }

                    if (states.OnComplete != null)
                    {
                        states.OnComplete(responseString);
                    }
                }
            }
            catch (Exception e)
            {
                if (states.OnError != null)
                {
                    states.OnError(e);
                }
            }
        }
示例#2
0
        private static void ResponseCallback(IAsyncResult result)
        {
            const int BufferLength = 8096;

            IDictionary <string, string> headers = new Dictionary <string, string>();
            IDictionary <string, string> cookies = new Dictionary <string, string>();

            StateContainer states    = (StateContainer)result.AsyncState;
            Exception      exception = null;

            try
            {
                HttpWebResponse response = (HttpWebResponse)states.Request.EndGetResponse(result);

                if (response.StatusCode < HttpStatusCode.OK && response.StatusCode >= HttpStatusCode.Ambiguous)
                {
                    throw new WebException(TextMessages.UnableToHandleTheRequest, null, WebExceptionStatus.UnknownError, response);
                }

                string contentType = response.ContentType;

                PopulateHeaders(response, headers);
                PopulateCookies(response, cookies);

                using (Stream stream = response.GetResponseStream())
                {
                    byte[] buffer = new byte[BufferLength];

                    string content;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        int bytesRead;

                        while ((bytesRead = stream.Read(buffer, 0, BufferLength)) > 0)
                        {
                            ms.Write(buffer, 0, bytesRead);
                        }

                        ms.Flush();

                        Encoding encoding = DetectEncoding(contentType, Encoding.UTF8);

                        content = encoding.GetString(ms.ToArray());
                    }

                    HttpResponse httpResponse = CreateHttpResponseWith(contentType, content, headers, cookies);

                    if (states.OnComplete != null)
                    {
                        states.OnComplete(httpResponse);
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
            }

            OnException(states, exception);
        }