public override void OnBeforeURLRequest(BeforeURLRequestParams parameters) { if ("POST" == parameters.Method) { PostData post = parameters.PostData; PostDataContentType contentType = post.ContentType; if (contentType == PostDataContentType.FORM_URL_ENCODED) { FormData postData = (FormData)post; postData.SetPair("key1", "value1", "value2"); postData.SetPair("key2", "value2"); } else if (contentType == PostDataContentType.MULTIPART_FORM_DATA) { MultipartFormData postData = (MultipartFormData)post; postData.SetPair("key1", "value1", "value2"); postData.SetPair("key2", "value2"); postData.SetFilePair("file3", "C:\\Test.zip"); } else if (contentType == PostDataContentType.PLAIN_TEXT) { RawData postData = (RawData)post; postData.Data = "raw data"; } else if (contentType == PostDataContentType.BYTES) { BytesData data = (BytesData)post; data.Data = Encoding.UTF8.GetBytes("My data"); } parameters.PostData = post; } }
///// <summary> ///// 判断是否为 gzip 请求 ///// 已经在请求的时候 自动处理 this._clientHander = new HttpClientHandler() { CookieContainer = this.Cookies, AutomaticDecompression = DecompressionMethods.GZip }; ///// </summary> ///// <param name="headers"></param> ///// <returns></returns> //private bool IsGzipRequest(HttpRequestHeaders headers) { // if (null != headers.AcceptEncoding && headers.AcceptEncoding.Any(x => x.Value.Contains("gzip"))) // { // return true; // } // return false; //} /// <summary> /// 对传递过来的 数据进行初始化 /// </summary> /// <param name="content"></param> /// <param name="dataType"></param> /// <param name="data"></param> private HttpContent InitHttpContent(HttpContent content, PostDataContentType dataType, Dictionary <string, string> data) { //处理需要提交的数据 if (null != data && data.Count > 0) { switch (dataType) { case PostDataContentType.Form: //直接将字典数据作为参数体 content = new FormUrlEncodedContent(data); break; case PostDataContentType.Json: //json数据 直接将首个数据作为传递数据 并进行转码 var realData = data.First().Value; // string decodeBody = HttpUtility.UrlEncode(realData); content = new StringContent(realData, Encoding.UTF8, "application/json"); //content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); break; case PostDataContentType.Binary: throw new NotImplementedException("未实现指定的功能....."); default: break; } } return(content); }
/// <summary> /// post 请求转发 /// 异步请求 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <param name="fromHeaders"></param> /// <returns></returns> public Task <HttpResponseMessage> PostAsync(string url, PostDataContentType dataType, Dictionary <string, object> data, NameValueCollection fromHeaders = null, CancellationToken?cancellationToken = null ) { CancellationToken token = CancellationToken.None; if (null != cancellationToken) { token = cancellationToken.Value; } if (null == this.Client) { this.Client = this.CreateNewHttpClient(); } //using (var handler = new HttpClientHandler() { CookieContainer = this.Cookies }) //using (var client = new HttpClient(handler)) //{ try { //修改请求对象的头信息 if (null != fromHeaders) { FormatRequestHeader(this.Client.DefaultRequestHeaders, fromHeaders); } //包装提交的数据 HttpContent content = null; content = this.InitHttpContent(content, dataType, data); var targetUri = new Uri(url); if (targetUri.Scheme == HttpSchemaOfHttps) { //开启 https 默认证书验证 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } //post 响应 。异步返回内容字符串 var tskResponse = this.Client.PostAsync(targetUri, content, token); return(tskResponse); } catch (Exception ex) { throw ex; } finally { this.Dispose(); } }
/// <summary> /// post 请求转发 /// 同步请求 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> public string PostRequestTransfer(string url, PostDataContentType dataType, Dictionary <string, string> data, NameValueCollection fromHeaders, CancellationToken cancellationToken) { try { var tskResponse = this.PostRequestTransferAsync(url, dataType, data, fromHeaders, cancellationToken); if (null == tskResponse) { return(string.Empty); } //等待 task执行完毕 返回结果 return(tskResponse.Result.Content.ReadAsStringAsync().Result); } catch (Exception ex) { throw ex; } }
/// <summary> /// post 请求转发 /// 同步请求 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> public string Post(string url, PostDataContentType dataType, Dictionary <string, object> data, NameValueCollection fromHeaders = null, CancellationToken?cancellationToken = null) { try { if (null == this.Client) { this.Client = this.CreateNewHttpClient(); } var tskResponse = this.PostAsync(url, dataType, data, fromHeaders, cancellationToken); if (null == tskResponse) { return(string.Empty); } //等待 task执行完毕 返回结果 return(tskResponse.Result.Content.ReadAsStringAsync().Result); } catch (Exception ex) { throw ex; } }
///// <summary> ///// 判断是否为 gzip 请求 ///// 已经在请求的时候 自动处理 this._clientHander = new HttpClientHandler() { CookieContainer = this.Cookies, AutomaticDecompression = DecompressionMethods.GZip }; ///// </summary> ///// <param name="headers"></param> ///// <returns></returns> //private bool IsGzipRequest(HttpRequestHeaders headers) { // if (null != headers.AcceptEncoding && headers.AcceptEncoding.Any(x => x.Value.Contains("gzip"))) // { // return true; // } // return false; //} /// <summary> /// 对传递过来的 数据进行初始化 /// </summary> /// <param name="content"></param> /// <param name="dataType"></param> /// <param name="data"></param> private HttpContent InitHttpContent(HttpContent content, PostDataContentType dataType, Dictionary <string, object> data) { //处理需要提交的数据 if (null != data && data.Count > 0) { switch (dataType) { case PostDataContentType.Form: //直接将字典数据作为参数体 var dicData = new Dictionary <string, string>(); foreach (var item in data) { dicData.Add(item.Key, item.Value.ToString()); } content = new FormUrlEncodedContent(dicData); break; case PostDataContentType.Json: //json数据 直接将首个数据作为传递数据 并进行转码 var realData = data.Values.ElementAt(0); string jsonData = string.Empty; if (realData != null) { jsonData = realData.ToJson(); } // string decodeBody = HttpUtility.UrlEncode(realData); content = new StringContent(jsonData, Encoding.UTF8, "application/json"); //content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); break; case PostDataContentType.Binary: throw new NotImplementedException("未实现指定的功能....."); default: break; } } return(content); }
public PostDataParam(string name, string filename, string value, PostDataContentType type) { if (String.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); if (String.IsNullOrEmpty(name)) throw new ArgumentNullException("filename"); this.Name = name; this.FileName = filename; this.Value = value; this.Type = type; }
public void AddFileParam(string name, string filename, string value, PostDataContentType type) { if (value != null && value.Length > 0) this.Params.Add(new PostDataParam(name, filename, value, type)); else return; }