You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.2 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Newtonsoft.Json;
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 DS.Module.Core.Helpers
{
/// <summary>
///
/// </summary>
public static class CommonFTPHelper
{
#region
/// <summary>
/// 转发EDI内部方法
/// </summary>
/// <param name="requestUrl">请求接口地址</param>
/// <param name="nameValueCollection">键值对参数</param>
/// <param name="fileInfo">文件信息</param>
/// <param name="contentType">默认 application/json</param>
/// <returns>返回结果</returns>
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 = JsonConvert.SerializeObject(new
{
message = $"{nameof(TransmitFtpFile)} 转发EDI内部方法异常原因{ex.Message}",
status = 0
});
}
}
return result;
}
#endregion
}
}