Exemple #1
0
        /// <summary>
        /// 获取相应对象
        /// </summary>
        /// <returns></returns>
        public HttpWebResponse GetResponse()
        {
            var timer = Stopwatch.StartNew();

            string[] ms = new string[3];
            if (Encoding == null)
            {
                Encoding = Encoding.UTF8;
            }
            ResponseCode = 0;
            Exception    = null;
            Uri uri = GetFullUrl();

            Trace.WriteLine(uri.ToString(), "HttpRequest.Url");

            var www = WebRequest.CreateHttp(uri);

            if (uri.Scheme == Uri.UriSchemeHttps)
            {
                www.ProtocolVersion = HttpVersion.Version10;
            }
            www.CookieContainer = Cookie;
            www.Timeout         = (int)Timeout.TotalMilliseconds;
            Headers.SetHeaders(www);
            www.Method    = GetMethod();
            www.KeepAlive = KeepAlive;
            try
            {
                _Abort          = www.Abort;
                _RequestCreated = DateTime.Now;
                if (_FormBody != null)
                {
                    FormBody.SetHeaders(www, Encoding);
                    var formdata = FormBody.GetBytes(Encoding);
                    www.ContentLength = formdata.Length;
                    if (formdata.Length > 0)
                    {
                        using (var req = www.GetRequestStream())
                        {
                            if (req.CanTimeout)
                            {
                                req.WriteTimeout = 3000;
                            }
                            req.Write(formdata, 0, formdata.Length);
                        }
                    }
                }
                else
                {
                    www.ContentLength = 0;
                }

                ms[0] = "set data:" + timer.ElapsedMilliseconds;
                timer.Restart();
                _RequestCreated = DateTime.Now;
                var res = (HttpWebResponse)www.GetResponse();
                _Abort = null;
                ms[1]  = "response:" + timer.ElapsedMilliseconds;
                timer.Restart();
                www.CookieContainer = new CookieContainer();
                www.CookieContainer.Add(res.Cookies);
                Headers.Clear();
                Headers.Add(res.Headers);
                ResponseCode = res.StatusCode;
                return(res);
            }
            catch (WebException ex)
            {
                _Abort    = null;
                Exception = ex;
                Trace.WriteLine(ex.Message, "HttpRequest.Error");
                var res = ex.Response as HttpWebResponse;
                ms[1] = "error:" + timer.ElapsedMilliseconds;
                timer.Restart();
                if (res != null)
                {
                    www.CookieContainer = new CookieContainer();
                    www.CookieContainer.Add(res.Cookies);
                    Headers.Clear();
                    Headers.Add(res.Headers);
                    ResponseCode = res.StatusCode;
                    return(res);
                }
                else
                {
                    ResponseCode = (HttpStatusCode)ex.Status;
                    return(null);
                }
                throw;
            }
            finally
            {
                ms[2] = "end:" + timer.ElapsedMilliseconds;
                Trace.WriteLine(ResponseCode, "HttpRequest.StatusCode");
                Trace.WriteLine(string.Join("; ", ms), "HttpRequest.Timing");
            }
        }
        /// <summary>
        /// 获取相应对象
        /// </summary>
        /// <returns></returns>
        public async Task <HttpResponseMessage> GetResponse()
        {
            var  timer        = Stopwatch.StartNew();
            long pre          = 0,
                 setheader    = 0,
                 setbody      = 0,
                 res          = 0,
                 acceptcookie = 0,
                 acceptheader = 0,
                 error        = 0,
                 end          = 0;

            if (Encoding == null)
            {
                Encoding = Encoding.UTF8;
            }
            if (PreRequest != null)
            {
                await PreRequest.ConfigureAwait(false);

                PreRequest = null;
                pre        = timer.ElapsedMilliseconds;
                timer.Restart();
            }
            ResponseCode = 0;
            Exception    = null;
            Uri uri = GetFullUrl();

            Trace.WriteLine(uri.ToString(), "HttpRequest.Url");

            var cancel = new CancellationTokenSource(Timeout);

            try
            {
                _Abort = cancel.Cancel;
                var message = new HttpRequestMessage(GetHttpMethod(), uri);

                if (Cookie?.Count > 0)
                {
                    message.Headers.Add("Cookie", Cookie.GetCookieHeader(uri));
                }
                if (KeepAlive)
                {
                    message.Headers.Connection.Add("keep-alive");
                }

                if (_FormBody != null)
                {
                    var formdata = FormBody.GetBytes(Encoding);
                    message.Content = new ByteArrayContent(formdata);

                    message.Content.Headers.Add("Content-Type", FormBody.ContentType);
                    message.Content.Headers.ContentType.CharSet = Encoding.WebName;
                    //message.Headers.TryAddWithoutValidation("Content-Type", "charset=" + Encoding.WebName);

                    setbody = timer.ElapsedMilliseconds;
                    timer.Restart();
                }

                if (_Headers?.Count > 0)
                {
                    foreach (var item in _Headers)
                    {
                        var arr = item.Value as IEnumerable <string>;
                        if (arr != null)
                        {
                            if (!message.Headers.TryAddWithoutValidation(item.Key, arr) &&
                                message.Content != null)
                            {
                                message.Content.Headers.TryAddWithoutValidation(item.Key, arr);
                            }
                        }
                        else
                        {
                            var str = item.Value as string ?? item.Value + "";
                            if (!message.Headers.TryAddWithoutValidation(item.Key, str) &&
                                message.Content != null)
                            {
                                message.Content.Headers.TryAddWithoutValidation(item.Key, str);
                            }
                        }
                    }
                    setheader = timer.ElapsedMilliseconds;
                    timer.Restart();
                }


                var response = await HttpClient.SendAsync(message, cancel.Token);

                _Abort = null;
                res    = timer.ElapsedMilliseconds;
                timer.Restart();
                if (AcceptCookie && response.Headers.Contains("Set-Cookie"))
                {
                    var setcookies = response.Headers.GetValues("Set-Cookie");
                    foreach (var cookie in setcookies)
                    {
                        Cookie.SetCookies(uri, cookie);
                    }
                    acceptcookie = timer.ElapsedMilliseconds;
                    timer.Restart();
                }
                if (AcceptHeader)
                {
                    Headers.Clear();
                    foreach (var head in response.Headers)
                    {
                        Headers.Add(head.Key, head.Value);
                    }
                    foreach (var head in response.Content.Headers)
                    {
                        Headers.Add(head.Key, head.Value);
                    }
                    acceptheader = timer.ElapsedMilliseconds;
                    timer.Restart();
                }
                ResponseCode = response.StatusCode;
                return(response);
            }
            catch (Exception ex)
            {
                error = timer.ElapsedMilliseconds;
                timer.Restart();
                if (ex is TaskCanceledException && _Abort != null)
                {
                    ex = new TimeoutException("请求已超时");
                }
                Trace.WriteLine(ex.Message, "HttpRequest.Error");
                Exception    = ex;
                ResponseCode = 0;
                return(null);
            }
            finally
            {
                cancel?.Dispose();
                _Abort  = null;
                setbody = timer.ElapsedMilliseconds;
                timer.Stop();
                Trace.WriteLine(ResponseCode, "HttpRequest.StatusCode");
                Trace.WriteLine(
                    $"pre={pre}; setheader={setheader}; setbody={setbody}; response={res}; acceptcookie={acceptcookie}; acceptheader={acceptheader}; error={error}; end={end}",
                    "HttpRequest.Timing");
            }
        }