Exemplo n.º 1
0
        private HttpResponse CreateErrorResponse(Exception ex)
        {
            var response = new HttpResponse();
            var webException = ex as WebException;
            if (webException != null && webException.Status == WebExceptionStatus.RequestCanceled)
            {
                response.ResponseStatus = _timeoutState.TimedOut ? ResponseStatus.TimedOut : ResponseStatus.Aborted;
                return response;
            }

            response.ErrorMessage = ex.Message;
            response.ErrorException = ex;
            response.ResponseStatus = ResponseStatus.Error;
            return response;
        }
Exemplo n.º 2
0
 private void ProcessResponseStream(Stream webResponseStream, HttpResponse response)
 {
     response.RawBytes = webResponseStream.ReadAsBytes();
 }
Exemplo n.º 3
0
 private void ProcessResponse(HttpResponse httpResponse, RequestAsyncHandle asyncHandle, Action<HttpResponse, RequestAsyncHandle> callback)
 {
     callback(httpResponse, asyncHandle);
 }
Exemplo n.º 4
0
        private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
        {
            using (webResponse)
            {
#if FRAMEWORK && !WIN8_1
                response.ContentEncoding = webResponse.ContentEncoding;
                response.Server = webResponse.Server;
#endif
                response.ContentType = webResponse.ContentType;
                response.ContentLength = webResponse.ContentLength;
                Stream webResponseStream = webResponse.GetResponseStream();

                ProcessResponseStream(webResponseStream, response);

                response.StatusCode = webResponse.StatusCode;
                response.StatusDescription = webResponse.StatusDescription;
                response.ResponseUri = webResponse.ResponseUri;
                response.ResponseStatus = ResponseStatus.Completed;

                webResponse.Close();
            }
        }
Exemplo n.º 5
0
        private static void ExecuteCallback(HttpResponse response, Action<HttpResponse> callback)
        {
            //using (Stream stream = response.GetResponseStream())
            //using (StreamReader reader = new StreamReader(stream))
            //{
            //    string contents = reader.ReadToEnd();

            //    callback(contents, null);
            //    /*//通过呼叫UI Thread来改变页面的显示 WINDOWPOHONE8
            //    Dispatcher.BeginInvoke(() =>
            //    {
            //        httpWebRequestTextBlock.Text = contents.ToString().Substring(begin + 7,
            //            end - begin - 7); textBox2.Text = note;
            //    });*/

            //}

            callback(response);
        }
Exemplo n.º 6
0
        private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback)
        {
            var response = new HttpResponse { ResponseStatus = ResponseStatus.None };

            try
            {
                if (_timeoutState.TimedOut)
                {
                    response.ResponseStatus = ResponseStatus.TimedOut;
                    ExecuteCallback(response, callback);
                    return;
                }

                GetRawResponseAsync(result, webResponse =>
                {
                    ExtractResponseData(response, webResponse);
                    ExecuteCallback(response, callback);
                });
            }
            catch (Exception ex)
            {
                ExecuteCallback(CreateErrorResponse(ex), callback);
            }
        }
Exemplo n.º 7
0
        private static void GetRawResponseAsync(IAsyncResult result, Action<HttpWebResponse> callback)
        {
            var response = new HttpResponse();
            response.ResponseStatus = ResponseStatus.None;

            HttpWebResponse raw = null;

            try
            {
                var webRequest = (HttpWebRequest)result.AsyncState;
                raw = webRequest.EndGetResponse(result) as HttpWebResponse;
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.RequestCanceled)
                {
                    throw ex;
                }

                // Check to see if this is an HTTP error or a transport error.
                // In cases where an HTTP error occurs ( status code >= 400 )
                // return the underlying HTTP response, otherwise assume a
                // transport exception (ex: connection timeout) and
                // rethrow the exception

                if (ex.Response is HttpWebResponse)
                {
                    raw = ex.Response as HttpWebResponse;
                }
                else
                {
                    throw ex;
                }
            }

            callback(raw);
            raw.Close();
        }
Exemplo n.º 8
0
        private void RequestStreamCallback(IAsyncResult result, Action<HttpResponse> callback)
        {
            var webRequest = (HttpWebRequest)result.AsyncState;

            if (_timeoutState.TimedOut)
            {
                var response = new HttpResponse { ResponseStatus = ResponseStatus.TimedOut };
                ExecuteCallback(response, callback);
                return;
            }

            // write body to request stream
            try
            {
                WriteRequestBody(webRequest, result);
            }
            catch (Exception ex)
            {
                ExecuteCallback(CreateErrorResponse(ex), callback);
                return;
            }

            IAsyncResult asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
            SetTimeout(asyncResult, _timeoutState);
        }