Net.Like.Xue.Tokyo/Assets/BITKit/Core/Extensions/HttpClient.cs

23 lines
660 B
C#
Raw Normal View History

2024-11-03 16:42:23 +08:00
using System.Net.Http;
using System.Text;
using System.Threading;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
namespace BITKit
{
public static class HttpClientExtensions
{
public static async UniTask<string> PostJsonAsync(this HttpClient self, string requestUrl, object value , CancellationToken cancellationToken=default)
{
var json = JsonConvert.SerializeObject(value);
2024-12-28 23:19:55 +08:00
if (value is string j)
{
json = j;
}
2024-11-03 16:42:23 +08:00
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response =await self.PostAsync(requestUrl, content, cancellationToken);
return await response.Content.ReadAsStringAsync();
}
}
}