28 lines
936 B
C#
28 lines
936 B
C#
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();
|
|
}
|
|
} |