示例#1
0
        /// <summary>
        /// 设置响应流
        /// </summary>
        /// <param name="stream"></param>
        internal void SetResponseStream(HttpStreamWrapper stream)
        {
            if (!HttpMonitor.RecordResponseContent || (HttpMonitor.MaxRecordContentSize > 0 && HttpMonitor.MaxRecordContentSize < stream.Length && stream.Length > 0))
            {
                return;
            }

            stream.EnableMirror();
            ResponseStream = stream.MirrorStream;
        }
示例#2
0
        private void GetResponseCallback(Action getResponseAction)
        {
            try
            {
                getResponseAction();
                Operation.Post(_ => OnResponseHeaderReceived(), null);
            }
            catch (WebException webex)
            {
                Exception = webex;
                if (webex.Status == WebExceptionStatus.ProtocolError)
                {
                    WebResponse = webex.Response as HttpWebResponse;
                    //发生错误,清掉期待类型以便于猜测
                    Request.ExceptType = null;
                }
            }
            catch (Exception ex)
            {
                Exception = ex;
            }

            if (Exception != null)
            {
                Operation.PostOperationCompleted(_ => OnRequestFailed(), null);
                return;
            }

            //保持环境
            Response = new HttpResponseMessage(WebResponse);
            Client.Setting.LastUri = WebResponse.ResponseUri;
            if (WebResponse.ResponseUri != WebRequest.Address)
            {
                Response.Redirection = new HttpRedirection(WebRequest.Address, WebResponse.ResponseUri);
            }
            else if (WebResponse.IsRedirectHttpWebResponse())
            {
                var location = Response.Location;
                if (location.IsNullOrEmpty())
                {
                    throw new ProtocolViolationException("是重定向的请求, 但是未提供新地址");
                }
                Response.Redirection = new HttpRedirection(WebRequest.Address, new Uri(WebRequest.Address, location));
            }
            if (Response.Cookies != null)
            {
                Client.CookieContainer.Add(Response.Cookies);
            }

            //处理响应
            if (Request.ExceptType == null)
            {
                //没有设置优选,先确定内容
                Response.Content = GetPreferedResponseContent();
            }
            else
            {
                Response.Content = Request.ExceptType;
            }
            //获得响应流
            Stream responseStream;

            try
            {
                responseStream = WebResponse.GetResponseStream();
            }
            catch (Exception ex)
            {
                WebResponse.Close();
                Exception = ex;
                _operation.PostOperationCompleted(_ => OnRequestFailed(), null);
                return;
            }

            //创建wrapper
            responseStream = new HttpStreamWrapper(responseStream, Response.ContentLength);
            (responseStream as HttpStreamWrapper).ProgressChanged += (s, e) => Operation.Post(_ => OnResponseReadProgressChanged(e), null);
            Operation.Post(_ => OnResponseStreamFetched(), null);

            //解压缩
            if (Request.AutoDecompressGzip)
            {
                if (Response.ContentEncoding.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                }
                else if (Response.ContentEncoding.IndexOf("deflate", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
                }
            }

            //读取响应
            try
            {
                ResponseContent.BeginProcessResponse(responseStream);
                OnRequestValidateResponse();
            }
            catch (Exception ex)
            {
                Exception = ex;
            }

            //跳转?
            if (Response.Redirection != null)
            {
                Operation.Post(_ => OnRequestRedirect(), null);
            }

            //完成
            WebResponse.Close();

            if (!IsSuccess)
            {
                Operation.PostOperationCompleted(_ => OnRequestFailed(), null);
            }
            else
            {
                Operation.PostOperationCompleted(_ => OnRequestFinished(), null);
            }
        }
示例#3
0
        /// <summary>
        /// 发送请求并做响应
        /// </summary>
        public void Send()
        {
            _operation = AsyncOperationManager.CreateOperation(null);

            Request.Normalize(Client, this);
            WebRequest = Client.HttpHandler.GetRequest(Request.Uri, Request.Method);
            Client.CopyDefaultSettings(this);
            Request.InitializeWebRequest(this);

            OnRequestSending();

            //如果有数据,则请求并写入
            if (Request.RequestData != null && Request.Method == HttpMethod.POST)
            {
                Request.RequestData.Prepare(WebRequest);

                if (Request.Async)
                {
                    WebRequest.BeginGetRequestStream(_ =>
                    {
                        var stream                   = WebRequest.EndGetRequestStream(_);
                        var embedStream              = new HttpStreamWrapper(stream, WebRequest.ContentLength);
                        embedStream.ProgressChanged += (s, e) =>
                                                       Operation.Post(_1 => OnRequestDataSendProgressChanged(e), null);

                        Operation.Post(s => OnRequestStreamFetched(), null);
                        Operation.Post(s => OnRequestDataSending(), null);

                        Request.RequestData.WriteTo(embedStream);
                        stream.Close();

                        Operation.Post(s => OnRequestDataSended(), null);
                        Operation.Post(s => OnRequestSended(), null);

                        WebRequest.BeginGetResponse(s => GetResponseCallback(() => { WebResponse = WebRequest.EndGetResponse(s) as HttpWebResponse; }), this);
                    }, this);

                    return;
                }
                else
                {
                    var stream      = WebRequest.GetRequestStream();
                    var embedStream = new HttpStreamWrapper(stream, WebRequest.ContentLength);
                    embedStream.ProgressChanged += (s, e) =>
                                                   Operation.Post(_1 => OnRequestDataSendProgressChanged(e), null);

                    OnRequestStreamFetched();
                    OnRequestDataSending();
                    Request.RequestData.WriteTo(embedStream);
                    stream.Close();
                    OnRequestDataSended();
                }
            }
            OnRequestSended();
            if (Request.Async)
            {
                WebRequest.BeginGetResponse(s => GetResponseCallback(() => { WebResponse = WebRequest.EndGetResponse(s) as HttpWebResponse; }), this);
            }
            else
            {
                GetResponseCallback(() => { WebResponse = WebRequest.GetResponse() as HttpWebResponse; });
            }
        }