mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2026-03-10 00:13:36 +08:00
refactor(tenpayv3)
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Flurl;
|
using Flurl;
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
|
||||||
using Flurl;
|
using Flurl;
|
||||||
using Flurl.Http;
|
using Flurl.Http;
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
|
||||||
using Flurl;
|
using Flurl;
|
||||||
using Flurl.Http;
|
using Flurl.Http;
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using System.Net.Http;
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
|
||||||
using Flurl;
|
using Flurl;
|
||||||
using Flurl.Http;
|
using Flurl.Http;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Flurl;
|
using Flurl;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Flurl;
|
using Flurl;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Flurl;
|
using Flurl;
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
|
||||||
using Flurl.Http;
|
using Flurl.Http;
|
||||||
|
|
||||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||||
@@ -40,17 +37,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|||||||
IFlurlRequest flurlReq = client
|
IFlurlRequest flurlReq = client
|
||||||
.CreateRequest(request, HttpMethod.Post, "marketing", "bank", "packages", request.PackageId, "tasks");
|
.CreateRequest(request, HttpMethod.Post, "marketing", "bank", "packages", request.PackageId, "tasks");
|
||||||
|
|
||||||
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, fileMetaJson: client.JsonSerializer.Serialize(request));
|
||||||
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
|
|
||||||
using var metaContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request)));
|
|
||||||
using var httpContent = new MultipartFormDataContent(boundary);
|
|
||||||
httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\"");
|
|
||||||
httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
|
||||||
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
||||||
metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
||||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
|
||||||
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
|
||||||
|
|
||||||
return await client.SendRequestAsync<Models.UploadMarketingBankPackagesTasksResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
return await client.SendRequestAsync<Models.UploadMarketingBankPackagesTasksResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
|
||||||
using Flurl.Http;
|
using Flurl.Http;
|
||||||
|
|
||||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||||
@@ -40,17 +37,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|||||||
IFlurlRequest flurlReq = client
|
IFlurlRequest flurlReq = client
|
||||||
.CreateRequest(request, HttpMethod.Post, "marketing", "favor", "media", "image-upload");
|
.CreateRequest(request, HttpMethod.Post, "marketing", "favor", "media", "image-upload");
|
||||||
|
|
||||||
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, fileMetaJson: client.JsonSerializer.Serialize(request));
|
||||||
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
|
|
||||||
using var metaContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request)));
|
|
||||||
using var httpContent = new MultipartFormDataContent(boundary);
|
|
||||||
httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\"");
|
|
||||||
httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
|
||||||
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
||||||
metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
||||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
|
||||||
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
|
||||||
|
|
||||||
return await client.SendRequestAsync<Models.UploadMarketingMediaImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
return await client.SendRequestAsync<Models.UploadMarketingMediaImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,17 +41,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|||||||
IFlurlRequest flurlReq = client
|
IFlurlRequest flurlReq = client
|
||||||
.CreateRequest(request, HttpMethod.Post, "merchant", "media", "upload");
|
.CreateRequest(request, HttpMethod.Post, "merchant", "media", "upload");
|
||||||
|
|
||||||
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, fileMetaJson: client.JsonSerializer.Serialize(request));
|
||||||
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
|
|
||||||
using var metaContent = new StringContent(client.JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
|
|
||||||
using var httpContent = new MultipartFormDataContent(boundary);
|
|
||||||
httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\"");
|
|
||||||
httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
|
||||||
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
||||||
metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
||||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
|
||||||
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
|
||||||
|
|
||||||
return await client.SendRequestAsync<Models.UploadMerchantMediaImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
return await client.SendRequestAsync<Models.UploadMerchantMediaImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,17 +71,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|||||||
IFlurlRequest flurlReq = client
|
IFlurlRequest flurlReq = client
|
||||||
.CreateRequest(request, HttpMethod.Post, "merchant", "media", "video_upload");
|
.CreateRequest(request, HttpMethod.Post, "merchant", "media", "video_upload");
|
||||||
|
|
||||||
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, fileMetaJson: client.JsonSerializer.Serialize(request));
|
||||||
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
|
|
||||||
using var metaContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request)));
|
|
||||||
using var httpContent = new MultipartFormDataContent(boundary);
|
|
||||||
httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\"");
|
|
||||||
httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
|
||||||
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
||||||
metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
||||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
|
||||||
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
|
||||||
|
|
||||||
return await client.SendRequestAsync<Models.UploadMerchantMediaVideoResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
return await client.SendRequestAsync<Models.UploadMerchantMediaVideoResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
|
||||||
using Flurl;
|
using Flurl;
|
||||||
using Flurl.Http;
|
using Flurl.Http;
|
||||||
|
|
||||||
@@ -138,6 +135,26 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|||||||
return await client.SendRequestWithJsonAsync<Models.SetMerchantServiceComplaintCompleteResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
return await client.SendRequestWithJsonAsync<Models.SetMerchantServiceComplaintCompleteResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// <para>异步调用 [POST] /merchant-service/complaints-v2/{complaint_id}/update-refund-progress 接口。</para>
|
||||||
|
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter10_2_19.shtml </para>
|
||||||
|
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter10_2_19.shtml </para>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client"></param>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<Models.UpdateMerchantServiceComplaintRefundProgressResponse> ExecuteUpdateMerchantServiceComplaintRefundProgressAsync(this WechatTenpayClient client, Models.UpdateMerchantServiceComplaintRefundProgressRequest request, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
||||||
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
||||||
|
|
||||||
|
IFlurlRequest flurlReq = client
|
||||||
|
.CreateRequest(request, HttpMethod.Post, "merchant-service", "complaints-v2", request.ComplaintId, "update-refund-progress");
|
||||||
|
|
||||||
|
return await client.SendRequestWithJsonAsync<Models.UpdateMerchantServiceComplaintRefundProgressResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// <para>异步调用 [POST] /merchant-service/complaint-notifications 接口。</para>
|
/// <para>异步调用 [POST] /merchant-service/complaint-notifications 接口。</para>
|
||||||
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter10_2_2.shtml </para>
|
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter10_2_2.shtml </para>
|
||||||
@@ -244,17 +261,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|||||||
IFlurlRequest flurlReq = client
|
IFlurlRequest flurlReq = client
|
||||||
.CreateRequest(request, HttpMethod.Post, "merchant-service", "images", "upload");
|
.CreateRequest(request, HttpMethod.Post, "merchant-service", "images", "upload");
|
||||||
|
|
||||||
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, fileMetaJson: client.JsonSerializer.Serialize(request));
|
||||||
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
|
|
||||||
using var metaContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request)));
|
|
||||||
using var httpContent = new MultipartFormDataContent(boundary);
|
|
||||||
httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\"");
|
|
||||||
httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
|
||||||
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
||||||
metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
||||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
|
||||||
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
|
||||||
|
|
||||||
return await client.SendRequestAsync<Models.UploadMerchantServiceImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
return await client.SendRequestAsync<Models.UploadMerchantServiceImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
|
|||||||
|
|
||||||
public class Detail : CreateHKTransactionAppRequest.Types.Detail
|
public class Detail : CreateHKTransactionAppRequest.Types.Detail
|
||||||
{
|
{
|
||||||
public new static class Types
|
public static new class Types
|
||||||
{
|
{
|
||||||
public class GoodsDetail : CreateHKTransactionAppRequest.Types.Detail.Types.GoodsDetail
|
public class GoodsDetail : CreateHKTransactionAppRequest.Types.Detail.Types.GoodsDetail
|
||||||
{
|
{
|
||||||
@@ -29,7 +29,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
|
|||||||
|
|
||||||
public class Scene : CreateHKTransactionAppRequest.Types.Scene
|
public class Scene : CreateHKTransactionAppRequest.Types.Scene
|
||||||
{
|
{
|
||||||
public new static class Types
|
public static new class Types
|
||||||
{
|
{
|
||||||
public class Store : CreateHKTransactionAppRequest.Types.Scene.Types.Store
|
public class Store : CreateHKTransactionAppRequest.Types.Scene.Types.Store
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
|
|||||||
|
|
||||||
public class Detail : CreateHKTransactionAppRequest.Types.Detail
|
public class Detail : CreateHKTransactionAppRequest.Types.Detail
|
||||||
{
|
{
|
||||||
public new static class Types
|
public static new class Types
|
||||||
{
|
{
|
||||||
public class GoodsDetail : CreateHKTransactionAppRequest.Types.Detail.Types.GoodsDetail
|
public class GoodsDetail : CreateHKTransactionAppRequest.Types.Detail.Types.GoodsDetail
|
||||||
{
|
{
|
||||||
@@ -29,7 +29,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
|
|||||||
|
|
||||||
public class Scene : CreateHKTransactionAppRequest.Types.Scene
|
public class Scene : CreateHKTransactionAppRequest.Types.Scene
|
||||||
{
|
{
|
||||||
public new static class Types
|
public static new class Types
|
||||||
{
|
{
|
||||||
public class Store : CreateHKTransactionAppRequest.Types.Scene.Types.Store
|
public class Store : CreateHKTransactionAppRequest.Types.Scene.Types.Store
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
public class Detail : CreateHKTransactionAppRequest.Types.Detail
|
public class Detail : CreateHKTransactionAppRequest.Types.Detail
|
||||||
{
|
{
|
||||||
public new static class Types
|
public static new class Types
|
||||||
{
|
{
|
||||||
public class GoodsDetail : CreateHKTransactionAppRequest.Types.Detail.Types.GoodsDetail
|
public class GoodsDetail : CreateHKTransactionAppRequest.Types.Detail.Types.GoodsDetail
|
||||||
{
|
{
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
public class Scene : CreateHKTransactionAppRequest.Types.Scene
|
public class Scene : CreateHKTransactionAppRequest.Types.Scene
|
||||||
{
|
{
|
||||||
public new static class Types
|
public static new class Types
|
||||||
{
|
{
|
||||||
public class Store : CreateHKTransactionAppRequest.Types.Scene.Types.Store
|
public class Store : CreateHKTransactionAppRequest.Types.Scene.Types.Store
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
|
|||||||
|
|
||||||
public class Detail : CreateHKTransactionAppRequest.Types.Detail
|
public class Detail : CreateHKTransactionAppRequest.Types.Detail
|
||||||
{
|
{
|
||||||
public new static class Types
|
public static new class Types
|
||||||
{
|
{
|
||||||
public class GoodsDetail : CreateHKTransactionAppRequest.Types.Detail.Types.GoodsDetail
|
public class GoodsDetail : CreateHKTransactionAppRequest.Types.Detail.Types.GoodsDetail
|
||||||
{
|
{
|
||||||
@@ -29,7 +29,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
|
|||||||
|
|
||||||
public class Scene : CreateHKTransactionAppRequest.Types.Scene
|
public class Scene : CreateHKTransactionAppRequest.Types.Scene
|
||||||
{
|
{
|
||||||
public new static class Types
|
public static new class Types
|
||||||
{
|
{
|
||||||
public class Store : CreateHKTransactionAppRequest.Types.Scene.Types.Store
|
public class Store : CreateHKTransactionAppRequest.Types.Scene.Types.Store
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using System.Web;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
|
||||||
|
{
|
||||||
|
internal static class FileHttpContentBuilder
|
||||||
|
{
|
||||||
|
public static MultipartFormDataContent Build(string fileName, byte[] fileBytes, string fileContentType, string fileMetaJson, string formDataName = "file")
|
||||||
|
{
|
||||||
|
return Build(fileName: fileName, fileBytes: fileBytes, fileContentType: fileContentType, fileMetaJson: fileMetaJson, formDataName: formDataName, (_) => { }, (_) => { });
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MultipartFormDataContent Build(string fileName, byte[] fileBytes, string fileContentType, string fileMetaJson, string formDataName, Action<HttpContent> configureMetaHttpContent, Action<HttpContent> configureFileHttpContent)
|
||||||
|
{
|
||||||
|
if (fileName == null) throw new ArgumentNullException(nameof(fileName));
|
||||||
|
if (fileMetaJson == null) throw new ArgumentNullException(nameof(fileMetaJson));
|
||||||
|
if (formDataName == null) throw new ArgumentNullException(nameof(formDataName));
|
||||||
|
if (configureFileHttpContent == null) throw new ArgumentNullException(nameof(configureFileHttpContent));
|
||||||
|
|
||||||
|
fileBytes = fileBytes ?? Array.Empty<byte>();
|
||||||
|
fileContentType = string.IsNullOrEmpty(fileContentType) ? "application/octet-stream" : fileContentType;
|
||||||
|
formDataName = formDataName.Replace("\"", "");
|
||||||
|
|
||||||
|
ByteArrayContent metaContent = new ByteArrayContent(Encoding.UTF8.GetBytes(fileMetaJson));
|
||||||
|
metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
||||||
|
configureMetaHttpContent(metaContent);
|
||||||
|
|
||||||
|
ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
|
||||||
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(fileContentType);
|
||||||
|
configureFileHttpContent(fileContent);
|
||||||
|
|
||||||
|
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
||||||
|
MultipartFormDataContent httpContent = new MultipartFormDataContent(boundary);
|
||||||
|
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/form-data; boundary={boundary}");
|
||||||
|
httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\"");
|
||||||
|
httpContent.Add(fileContent, $"\"{formDataName}\"", $"\"{HttpUtility.UrlEncode(fileName)}\"");
|
||||||
|
return httpContent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user