Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        /// <summary>创建头部</summary>
        /// <param name="length"></param>
        /// <returns></returns>
        protected override String BuildHeader(Int32 length)
        {
            if (Method.IsNullOrEmpty())
            {
                Method = length > 0 ? "POST" : "GET";
            }

            // 分解主机和资源
            var host = "";
            var uri  = Url;

            if (uri == null)
            {
                uri = new Uri("/");
            }

            if (uri.Scheme.EqualIgnoreCase("http", "ws"))
            {
                if (uri.Port == 80)
                {
                    host = uri.Host;
                }
                else
                {
                    host = "{0}:{1}".F(uri.Host, uri.Port);
                }
            }
            else if (uri.Scheme.EqualIgnoreCase("https"))
            {
                if (uri.Port == 443)
                {
                    host = uri.Host;
                }
                else
                {
                    host = "{0}:{1}".F(uri.Host, uri.Port);
                }
            }

            // 构建头部
            var sb = Pool.StringBuilder.Get();

            sb.AppendFormat("{0} {1} HTTP/1.1\r\n", Method, uri.PathAndQuery);
            sb.AppendFormat("Host:{0}\r\n", host);

            if (!Accept.IsNullOrEmpty())
            {
                sb.AppendFormat("Accept:{0}\r\n", Accept);
            }
            if (Compressed)
            {
                sb.AppendLine("Accept-Encoding:gzip, deflate");
            }
            if (!AcceptLanguage.IsNullOrEmpty())
            {
                sb.AppendFormat("AcceptLanguage:{0}\r\n", AcceptLanguage);
            }
            if (!UserAgent.IsNullOrEmpty())
            {
                sb.AppendFormat("User-Agent:{0}\r\n", UserAgent);
            }

            // 内容长度
            if (length > 0)
            {
                sb.AppendFormat("Content-Length:{0}\r\n", length);
            }
            if (!ContentType.IsNullOrEmpty())
            {
                sb.AppendFormat("Content-Type:{0}\r\n", ContentType);
            }

            if (KeepAlive)
            {
                sb.AppendLine("Connection:keep-alive");
            }
            if (!Referer.IsNullOrEmpty())
            {
                sb.AppendFormat("Referer:{0}\r\n", Referer);
            }

            foreach (var item in Headers)
            {
                sb.AppendFormat("{0}:{1}\r\n", item.Key, item.Value);
            }

            sb.AppendLine();

            return(sb.Put(true));
        }