/// <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); }
/// <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); }
/// <summary> /// 初始化请求 /// </summary> /// <param name="context"></param> internal void InitializeWebRequest(HttpContext context) { var request = context.WebRequest; if (!Accept.IsNullOrEmpty()) { request.Accept = Accept; } if (!ContentType.IsNullOrEmpty()) { request.ContentType = ContentType; } else if (Method == HttpMethod.POST) { request.ContentType = "application/x-www-form-urlencoded"; } if (IfModifiedSince != null) { request.IfModifiedSince = this.IfModifiedSince.Value; } if (Range != null) { request.AddRange(this.Range.Value.Key, this.Range.Value.Value); } if (AppendAjaxHeader) { request.Headers.Add("X-Requested-With", "XMLHttpRequest"); } request.KeepAlive = KeepAlive; if (Timeout > 0) { request.Timeout = request.ReadWriteTimeout = Timeout.Value; } #if NET4 if (!string.IsNullOrEmpty(Host)) { request.Host = Host; Host = null; } #endif request.AllowAutoRedirect = AllowAutoRedirect; request.AutomaticDecompression = DecompressionMethods.None; if (ReferUri != null) { request.Referer = ReferUri.ToString(); } }
/// <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); }
/// <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="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)); }
/// <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)); }