/// <summary> /// 给.NET Core使用的HttpPost请求公共设置方法 /// </summary> /// <param name="url"></param> /// <param name="hc"></param> /// <param name="cookieContainer"></param> /// <param name="postStream"></param> /// <param name="fileDictionary">需要上传的文件,Key:对应要上传的Name,Value:本地文件名,或文件内容的Base64编码</param> /// <param name="refererUrl"></param> /// <param name="encoding"></param> /// <param name="certName">证书唯一名称,如果不需要则保留null</param> /// <param name="useAjax"></param> /// <param name="headerAddition">header附加信息</param> /// <param name="timeOut"></param> /// <param name="checkValidationResult"></param> /// <param name="contentType"></param> /// <returns></returns> public static HttpClient HttpPost_Common_NetCore(string url, out HttpContent hc, CookieContainer cookieContainer = null, Stream postStream = null, Dictionary <string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null, string certName = null, bool useAjax = false, Dictionary <string, string> headerAddition = null, int timeOut = Config.TIME_OUT, bool checkValidationResult = false, string contentType = HttpClientHelper.DEFAULT_CONTENT_TYPE) { //HttpClientHandler handler = HttpClientHelper.GetHttpClientHandler(cookieContainer, SenparcHttpClientWebProxy, DecompressionMethods.GZip); //if (checkValidationResult) //{ // handler.ServerCertificateCustomValidationCallback = new Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool>(CheckValidationResult); //} //if (cer != null) //{ // handler.ClientCertificates.Add(cer); //} //TODO:此处 handler并没有被使用到,因此 cer 实际无法传递(这个也是 .net core 目前针对多 cer 场景的一个问题) var senparcHttpClient = SenparcHttpClient.GetInstanceByName(certName); senparcHttpClient.SetCookie(new Uri(url), cookieContainer);//设置Cookie HttpClient client = senparcHttpClient.Client; HttpClientHeader(client, refererUrl, useAjax, headerAddition, timeOut); #region 处理Form表单文件上传 var formUploadFile = fileDictionary != null && fileDictionary.Count > 0;//是否用Form上传文件 if (formUploadFile) { contentType = "multipart/form-data"; //通过表单上传文件 string boundary = "----" + SystemTime.Now.Ticks.ToString("x"); var multipartFormDataContent = new MultipartFormDataContent(boundary); hc = multipartFormDataContent; foreach (var file in fileDictionary) { try { var fileNameOrFileData = file.Value; var formFileData = new FormFileData(fileNameOrFileData); string fileName = null; //准备文件流 var memoryStream = new MemoryStream();//这里不能释放,否则如在请求的时候 memoryStream 已经关闭会发生错误 if (formFileData.TryLoadStream(memoryStream).ConfigureAwait(false).GetAwaiter().GetResult()) { //fileNameOrFileData 中储存的储存的是 Stream fileName = Path.GetFileName(formFileData.GetAvaliableFileName(SystemTime.NowTicks.ToString())); } else { //fileNameOrFileData 中储存的储存的可能是文件地址或备注 using (var fileStream = FileHelper.GetFileStream(fileNameOrFileData)) { if (fileStream != null) { //存在文件 fileStream.CopyTo(memoryStream);//TODO:可以使用异步方法 fileName = Path.GetFileName(fileNameOrFileData); fileStream.Dispose(); } else { //只是注释 multipartFormDataContent.Add(new StringContent(file.Value), "\"" + file.Key + "\""); } } } if (memoryStream.Length > 0) { //有文件内容 //multipartFormDataContent.Add(new StreamContent(memoryStream), file.Key, Path.GetFileName(fileName)); //报流已关闭的异常 memoryStream.Seek(0, SeekOrigin.Begin); multipartFormDataContent.Add(CreateFileContent(memoryStream, file.Key, fileName), file.Key, fileName); } } catch (Exception ex) { throw ex; } } hc.Headers.ContentType = MediaTypeHeaderValue.Parse(string.Format("multipart/form-data; boundary={0}", boundary)); } else { if (postStream.Length > 0) { if (contentType == HttpClientHelper.DEFAULT_CONTENT_TYPE) { //如果ContentType是默认值,则设置成为二进制流 contentType = "application/octet-stream"; } //contentType = "application/x-www-form-urlencoded"; } hc = new StreamContent(postStream); hc.Headers.ContentType = new MediaTypeHeaderValue(contentType); //使用Url格式Form表单Post提交的时候才使用application/x-www-form-urlencoded //去掉注释以测试Request.Body为空的情况 //hc.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); } //HttpContentHeader(hc, timeOut); #endregion if (!string.IsNullOrEmpty(refererUrl)) { client.DefaultRequestHeaders.Referrer = new Uri(refererUrl); } return(client); }
/// <summary> /// 给.NET Framework使用的HttpPost请求公共设置方法 /// </summary> /// <param name="url"></param> /// <param name="cookieContainer"></param> /// <param name="postStream"></param> /// <param name="fileDictionary">需要上传的文件,Key:对应要上传的Name,Value:本地文件名,或文件内容的Base64编码</param> /// <param name="refererUrl"></param> /// <param name="encoding"></param> /// <param name="cer"></param> /// <param name="useAjax"></param> /// <param name="headerAddition">header附加信息</param> /// <param name="timeOut"></param> /// <param name="checkValidationResult"></param> /// <param name="contentType"></param> /// <returns></returns> public static HttpWebRequest HttpPost_Common_Net45(string url, CookieContainer cookieContainer = null, Stream postStream = null, Dictionary <string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null, X509Certificate2 cer = null, bool useAjax = false, Dictionary <string, string> headerAddition = null, int timeOut = Config.TIME_OUT, bool checkValidationResult = false, string contentType = HttpClientHelper.DEFAULT_CONTENT_TYPE) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = timeOut; request.Proxy = _webproxy; if (cer != null) { request.ClientCertificates.Add(cer); } if (checkValidationResult) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } #region 处理Form表单文件上传 var formUploadFile = fileDictionary != null && fileDictionary.Count > 0;//是否用Form上传文件 if (formUploadFile) { contentType = "multipart/form-data"; //通过表单上传文件 string boundary = "----" + SystemTime.Now.Ticks.ToString("x"); postStream = postStream ?? new MemoryStream(); //byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); string fileFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; string dataFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (var file in fileDictionary) { try { var fileNameOrFileData = file.Value; var formFileData = new FormFileData(fileNameOrFileData); string formdata = null; using (var memoryStream = new MemoryStream()) { if (formFileData.TryLoadStream(memoryStream).ConfigureAwait(false).GetAwaiter().GetResult()) { //fileNameOrFileData 中储存的储存的是 Stream var fileName = Path.GetFileName(formFileData.GetAvaliableFileName(SystemTime.NowTicks.ToString())); formdata = string.Format(fileFormdataTemplate, file.Key, fileName); } else { //fileNameOrFileData 中储存的储存的可能是文件地址或备注 //准备文件流 using (var fileStream = FileHelper.GetFileStream(fileNameOrFileData)) { if (fileStream != null) { //存在文件 memoryStream.Seek(0, SeekOrigin.Begin); fileStream.CopyTo(memoryStream); formdata = string.Format(fileFormdataTemplate, file.Key, Path.GetFileName(fileNameOrFileData)); fileStream.Dispose(); } else { //不存在文件或只是注释 formdata = string.Format(dataFormdataTemplate, file.Key, file.Value); } } } //统一处理 var formdataBytes = Encoding.UTF8.GetBytes(postStream.Length == 0 ? formdata.Substring(2, formdata.Length - 2) : formdata);//第一行不需要换行 postStream.Write(formdataBytes, 0, formdataBytes.Length); //写入文件 if (memoryStream.Length > 0) { memoryStream.Seek(0, SeekOrigin.Begin); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = memoryStream.Read(buffer, 0, buffer.Length)) != 0) { postStream.Write(buffer, 0, bytesRead); } } } } catch (Exception ex) { throw ex; } } //结尾 var footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); postStream.Write(footer, 0, footer.Length); //request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);//request.ContentType在下方统一设置 contentType = string.Format("multipart/form-data; boundary={0}", boundary); } else { if (postStream.Length > 0) { if (contentType == HttpClientHelper.DEFAULT_CONTENT_TYPE) { //如果ContentType是默认值,则设置成为二进制流 contentType = "application/octet-stream"; } //contentType = "application/x-www-form-urlencoded"; } } #endregion request.ContentType = contentType; request.ContentLength = postStream != null ? postStream.Length : 0; HttpClientHeader(request, refererUrl, useAjax, headerAddition, timeOut); if (cookieContainer != null) { request.CookieContainer = cookieContainer; } return(request); }