Пример #1
0
        /// <summary>创建客户端会话</summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        protected virtual HttpWebRequest Create(String uri)
        {
            var req = WebRequest.Create(uri) as HttpWebRequest;

            req.UserAgent = UserAgent;

            if (AutomaticDecompression != DecompressionMethods.None)
            {
                req.AutomaticDecompression = AutomaticDecompression;
            }

            if (!Accept.IsNullOrEmpty())
            {
                req.Accept = Accept;
            }
            if (!AcceptLanguage.IsNullOrEmpty())
            {
                req.Headers[HttpRequestHeader.AcceptLanguage + ""] = AcceptLanguage;
            }
            if (!Referer.IsNullOrEmpty())
            {
                req.Referer = Referer;
            }

            return(req);
        }
Пример #2
0
        /// <summary>创建客户端会话</summary>
        /// <returns></returns>
        public virtual HttpClientX EnsureCreate()
        {
            var http = _client;

            if (http == null)
            {
                var p = Proxy;
                if (p == null && !ProxyAddress.IsNullOrEmpty())
                {
                    Proxy = p = new WebProxy(ProxyAddress);
                }
                if (p == null)
                {
                    http = new HttpClientX();
                }
                else
                {
                    http = new HttpClientX(new HttpClientHandler {
                        Proxy = p
                    });
                }

                _client      = http;
                Request      = http.DefaultRequestHeaders;
                http.Timeout = new TimeSpan(0, 0, 0, 0, Timeout);
            }

            var req = http.DefaultRequestHeaders;

            if (!UserAgent.IsNullOrEmpty())
            {
                req.UserAgent.ParseAdd(UserAgent);
            }
            if (!Accept.IsNullOrEmpty())
            {
                req.Accept.ParseAdd(Accept);
            }
            if (!AcceptLanguage.IsNullOrEmpty())
            {
                req.AcceptLanguage.ParseAdd(AcceptLanguage);
            }
            if (AutomaticDecompression != DecompressionMethods.None)
            {
                req.AcceptEncoding.ParseAdd("gzip, deflate");
            }
            if (!Referer.IsNullOrEmpty())
            {
                req.Referrer = new Uri(Referer);
            }

            GetCookie();

            return(http);
        }
Пример #3
0
        protected override System.Net.WebRequest GetWebRequest(System.Uri address)
        {
            _Url = address;

            var basereq = base.GetWebRequest(address);

            System.Net.HttpWebRequest req = basereq as System.Net.HttpWebRequest;
            if (req != null)
            {
                if (ContentType != null)
                {
                    req.ContentType = ContentType;
                }
                req.AllowAutoRedirect      = false;
                req.CookieContainer        = Cookies;
                req.AutomaticDecompression = System.Net.DecompressionMethods.GZip | DecompressionMethods.Deflate;
                if (Timeout > 0)
                {
                    req.Timeout = Timeout * 1000;
                }
                if (!Referer.IsNullOrEmpty())
                {
                    req.Referer = Referer;
                }
                if (!UserAgent.IsNullOrEmpty())
                {
                    req.UserAgent = UserAgent;
                }
                if (IfModifiedSince != null)
                {
                    req.IfModifiedSince = IfModifiedSince.Value;
                }
                req.KeepAlive = KeepAlive;

                req.Accept = Headers[HttpRequestHeader.Accept].NotEmpty(Headers["accept"], req.Accept);

                if (OnGetWebRequest != null)
                {
                    OnGetWebRequest(address, req);
                }
            }
            return(basereq);
        }
Пример #4
0
        /// <summary>创建客户端会话</summary>
        /// <returns></returns>
        protected virtual HttpClientX Create()
        {
            var http = _client;

            if (http == null)
            {
                http         = _client = new HttpClientX();
                Request      = http.DefaultRequestHeaders;
                http.Timeout = new TimeSpan(0, 0, 0, 0, Timeout);
            }

            var req = http.DefaultRequestHeaders;

            if (!UserAgent.IsNullOrEmpty())
            {
                req.UserAgent.ParseAdd(UserAgent);
            }
            if (!Accept.IsNullOrEmpty())
            {
                req.Accept.ParseAdd(Accept);
            }
            if (!AcceptLanguage.IsNullOrEmpty())
            {
                req.AcceptLanguage.ParseAdd(AcceptLanguage);
            }
            if (AutomaticDecompression != DecompressionMethods.None)
            {
                req.AcceptEncoding.ParseAdd("gzip, deflate");
            }
            if (!Referer.IsNullOrEmpty())
            {
                req.Referrer = new Uri(Referer);
            }

            GetCookie();

            return(http);
        }
Пример #5
0
        /// <summary>创建客户端会话</summary>
        /// <returns></returns>
        public virtual HttpClient EnsureCreate()
        {
            var http = _client;

            if (http == null)
            {
                var p = Proxy;
                if (p == null && !ProxyAddress.IsNullOrEmpty())
                {
                    Proxy = p = new WebProxy(ProxyAddress);
                }

                var handler = new HttpClientHandler();
                if (p != null)
                {
                    handler.UseProxy = true;
                    handler.Proxy    = p;
                }
                else
                {
                    handler.UseProxy = false;
                    handler.Proxy    = null;
                }
                if (AutomaticDecompression != DecompressionMethods.None)
                {
                    handler.AutomaticDecompression = AutomaticDecompression;
                }

                http = new HttpClient(handler);

                _client      = http;
                Request      = http.DefaultRequestHeaders;
                http.Timeout = new TimeSpan(0, 0, 0, 0, Timeout);
            }

            var req = http.DefaultRequestHeaders;

            if (!UserAgent.IsNullOrEmpty())
            {
                req.UserAgent.ParseAdd(UserAgent);
            }
            if (!Accept.IsNullOrEmpty())
            {
                req.Accept.ParseAdd(Accept);
            }
            if (!AcceptLanguage.IsNullOrEmpty())
            {
                req.AcceptLanguage.ParseAdd(AcceptLanguage);
            }
            if (AutomaticDecompression != DecompressionMethods.None)
            {
                req.AcceptEncoding.ParseAdd("gzip, deflate");
            }
            if (!Referer.IsNullOrEmpty())
            {
                req.Referrer = new Uri(Referer);
            }
            if (KeepAlive)
            {
                req.Connection.ParseAdd("keep-alive");
            }

            GetCookie(http);

            return(http);
        }
        /// <summary>
        /// 初始化请求
        /// </summary>
        /// <param name="context"></param>
        internal void InitializeWebRequest([NotNull] HttpContext context)
        {
            var request = context.WebRequest;

            if (!Accept.IsNullOrEmpty())
            {
                request.Accept = Accept;
            }
            if (!UserAgent.IsNullOrEmpty())
            {
                request.UserAgent = UserAgent;
            }
            if (!AcceptEncoding.IsNullOrEmpty())
            {
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, AcceptEncoding);
            }
            request.ServicePoint.Expect100Continue = false;
            request.KeepAlive = KeepAlive;
            request.ServicePoint.UseNagleAlgorithm = UseNagleAlgorithm;
            request.AllowWriteStreamBuffering      = AllowWriteStreamBuffering;
            Authorization?.SetRequest(context.WebRequest, context);

            if (Range != null)
            {
                var range = Range.Value;
#if NET_GT_4
                if (range.Value.HasValue)
                {
                    request.AddRange(range.Key, range.Value.Value);
                }
                else
                {
                    request.AddRange(range.Key);
                }
#else
                if (range.Value.HasValue)
                {
                    request.AddRange(range.Key, range.Value.Value);
                }
                else
                {
                    request.AddRange(range.Key);
                }
#endif
            }

            if (AppendAjaxHeader)
            {
                request.Headers.Add("X-Requested-With", "XMLHttpRequest");
            }

            request.KeepAlive = KeepAlive;

            if (Timeout.HasValue)
            {
                request.Timeout = Timeout.Value;
            }
            if (ReadWriteTimeout.HasValue)
            {
                request.ReadWriteTimeout = ReadWriteTimeout.Value;
            }
            if (!TransferEncoding.IsNullOrEmpty())
            {
                request.TransferEncoding = TransferEncoding;
                request.SendChunked      = true;
            }

#if NET_GT_4
            if (!string.IsNullOrEmpty(Host))
            {
                request.Host = Host;
            }
#endif
            request.AllowAutoRedirect      = AllowAutoRedirect;
            request.AutomaticDecompression = DecompressionMethods.None;
#pragma warning disable 618
            if (ReferUri != null)
            {
                request.Referer = ReferUri.OriginalString;
            }
#pragma warning restore 618
            if (!Referer.IsNullOrEmpty())
            {
                request.Referer = Referer;
            }

            if (HttpRequestCacheLevel != null)
            {
                request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Value);
            }
            else if (HttpCacheAgeControl != null)
            {
                request.CachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.Value, MaxAge ?? TimeSpan.Zero, AgeOrFreshOrStale ?? TimeSpan.Zero, SyncTime ?? DateTime.Now);
            }
            if (IfModifiedSince != null)
            {
                request.IfModifiedSince = IfModifiedSince.Value;
            }

            //copy headers
            if (Headers != null)
            {
                foreach (var header in Headers.AllKeys)
                {
                    request.Headers[header] = Headers[header];
                }
            }

            if (_proxySet)
            {
                request.Proxy = _webProxy;
            }
            request.PreAuthenticate = PreAuthenticate;

            //初始化兼容参数
            if (request.Proxy != null)
            {
                if (Context.Client.Setting.ForceStreamBufferWithProxy)
                {
                    //如果有代理,则默认允许流缓冲。如果不允许,则很可能会导致代理身份验证失败。
                    request.AllowWriteStreamBuffering = true;
                }
                if (request.Proxy is WebProxy)
                {
                    var proxy = request.Proxy as WebProxy;
                    if (!proxy.UseDefaultCredentials && proxy.Credentials is NetworkCredential && request.Headers["Proxy-Authorization"].IsNullOrEmpty())
                    {
                        var cred = proxy.Credentials as NetworkCredential;
                        request.Headers.Set(HttpRequestHeader.ProxyAuthorization, "Basic " + Convert.ToBase64String(Encoding.GetBytes(cred.UserName + ":" + cred.Password)));
                    }
                }
            }

            //如果设置了证书,则添加
            if (X509Certificates?.Length > 0)
            {
                request.ClientCertificates.AddRange(X509Certificates);
            }

            OnPostInitRequest(new PostInitRequestEventArgs(request, context, context.Client));
        }