This commit is contained in:
CortexCore
2024-07-12 19:32:02 +08:00
commit 170f05af0c
13 changed files with 440 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
using System.Text;
using Newtonsoft.Json;
namespace WeChatSharp.Services;
public class WeChatHttpClient
{
private readonly WeChatAccessTokenService _accessTokenService;
private readonly HttpClient _httpClient;
public WeChatHttpClient(WeChatAccessTokenService accessTokenService, HttpClient httpClient)
{
_accessTokenService = accessTokenService;
_httpClient = httpClient;
}
public async Task<string> PostJsonAsync<T>(string url,T model)
{
var json = JsonConvert.SerializeObject(model);
var jsonContent = new StringContent(json, Encoding.UTF8, "application/json");
return await PostAsync(url, jsonContent);
}
public async Task<string> PostAsync(string url,HttpContent content)
{
url += $"?{WeChatAccessTokenService._AccessToken}={await _accessTokenService.GetAccessTokenAsync()}";
var responseMessage = await _httpClient.PostAsync(url, content);
return await responseMessage.Content.ReadAsStringAsync();
}
}