using Furion.JsonSerialization; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using System.Web; namespace Myshipping.Application.Helper { public static class FTPHelper { #region /// /// 转发EDI内部方法 /// /// 请求接口地址 /// 键值对参数 /// 文件信息 /// 默认 application/json /// 返回结果 public static string TransmitFtpFile(string requestUrl, NameValueCollection nameValueCollection, dynamic fileInfo, string contentType = "application/json") { var result = string.Empty; using (var httpClient = new HttpClient()) { try { using (var reduceAttach = new MultipartFormDataContent()) { string[] allKeys = nameValueCollection.AllKeys; foreach (string key in allKeys) { var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(nameValueCollection[key])); dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue($"form-data") { Name = key }; reduceAttach.Add(dataContent); } #region 文件参数 if (fileInfo != null) { var Content = new ByteArrayContent(fileInfo.fileBytes); //Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") //{ // Name = fileInfo.file.ToString(), // FileName = fileInfo.fileName.ToString(), //}; Content.Headers.Add("Content-Type", contentType); reduceAttach.Add(Content, fileInfo.file.ToString(), HttpUtility.UrlEncode(fileInfo.fileName.ToString())); } #endregion //请求 var response = httpClient.PostAsync(requestUrl, reduceAttach).Result; result = response.Content.ReadAsStringAsync().Result; } } catch (Exception ex) { result = JSON.Serialize(new { message = $"{nameof(TransmitFtpFile)} 转发EDI内部方法异常,原因:{ex.Message}", status = 0 }); } } return result; } #endregion } }