/// <summary> /// 新建并初始化一个Http请求 /// </summary> /// <param name="url">请求地址</param> /// <param name="method">请求方法</param> private void Create(string url, RequestMethod method) { _Request = (HttpWebRequest)WebRequest.Create(url); _Request.Method = method.ToString(); _Request.Accept = "application/json"; _Request.ContentType = ContentType; if (_Token != null) { _Request.Headers.Add(HttpRequestHeader.Authorization, _Token); } if (AcceptEncoding != CompressType.None) { _Request.Headers.Add(HttpRequestHeader.AcceptEncoding, AcceptEncoding.ToString().ToLower()); } if (ContentEncoding != CompressType.None) { _Request.Headers.Add(HttpRequestHeader.ContentEncoding, ContentEncoding.ToString().ToLower()); } if (Headers == null) { return; } foreach (var header in Headers) { _Request.Headers[header.Key] = header.Value; } }
internal void SetRequestHeaders(HttpRequestMessage request) { request.Headers.Clear(); Accept.ForEach(x => request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(x))); AcceptEncoding.ForEach(x => request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(x))); CustomHeaders.ToList().ForEach(x => request.Headers.Add(x.Key, x.Value)); if (request.Content != null) { request.Content.Headers.ContentType = new MediaTypeHeaderValue(ContentType); } }
public RequestEnvelope(RequestEnvelope other) : this() { this.correlationId_ = other.correlationId_; this.serviceName_ = other.serviceName_; this.methodName_ = other.methodName_; this.payload_ = other.payload_; this.authId_ = other.authId_; this.authToken_ = other.authToken_; this.clientVersion_ = other.clientVersion_; this.clientStartupTimestamp_ = other.clientStartupTimestamp_; this.platform_ = other.platform_; this.region_ = other.region_; this.clientExternalVersion_ = other.clientExternalVersion_; this.clientInternalVersion_ = other.clientInternalVersion_; this.requestId_ = other.requestId_; this.acceptEncoding_ = other.acceptEncoding_; this.flag_ = other.flag_.Clone(); this.telemetryEvent_ = other.telemetryEvent_.Clone(); this.currentClientTime_ = other.currentClientTime_; this.nimbleSessionId_ = other.nimbleSessionId_; this.timezone_ = other.timezone_; this.firmwareVersion_ = other.firmwareVersion_; this.carrier_ = other.carrier_; this.networkAccess_ = other.networkAccess_; this.hardwareId_ = other.hardwareId_; this.advertiserId_ = other.advertiserId_; this.vendorId_ = other.vendorId_; this.androidId_ = other.androidId_; this.jailbrokenFlag_ = other.jailbrokenFlag_; this.piracyFlag_ = other.piracyFlag_; this.synergyId_ = other.synergyId_; this.deviceModel_ = other.deviceModel_; this.deviceId_ = other.deviceId_; this.application_ = other.application_; this._unknownFields = UnknownFieldSet.Clone(other._unknownFields); }
/// <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)); }