Exemple #1
0
        /// <summary>从本地获取Cookie并设置到Http请求头</summary>
        private void GetCookie(HttpClientX http)
        {
            var req = http.DefaultRequestHeaders;

            if (req == null)
            {
                return;
            }

            if (Cookie == null || Cookie.Count == 0)
            {
                return;
            }

            var sb = new StringBuilder();

            foreach (var item in Cookie)
            {
                if (sb.Length > 0)
                {
                    sb.Append(";");
                }
                sb.AppendFormat("{0}={1}", item.Key, item.Value);
            }
            req.Add("Cookie", sb.ToString());
        }
Exemple #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(http);

            return(http);
        }
Exemple #3
0
        private HttpClientX Check(String address)
        {
            var uri = new Uri(address);

            if (_client == null)
            {
                _client = Create(uri);
            }
            // 远程主机不同,需要重新建立
            else if (_client.Remote + "" != uri + "")
            {
                _client.Dispose();
                _client = Create(uri);
            }

            //_client.Url = new Uri(address);

            return(_client);
        }