public void GetTeaplaIDNew(string InputJson, string appid, string secret)
{
try
{
HttpClient client = new HttpClient();
// Get access token
string GetPageAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
var tokenResponse = client.GetAsync(GetPageAccessTokenUrl).Result;
tokenResponse.EnsureSuccessStatusCode(); // Ensure success or throw exception
string tokenJson = tokenResponse.Content.ReadAsStringAsync().Result;
Wxchat wxchats = JsonConvert.DeserializeObject<Wxchat>(tokenJson);
// Send message
HttpContent httpContent = new StringContent(InputJson);
string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + wxchats.access_token;
var messageResponse = client.PostAsync(url, httpContent).Result;
messageResponse.EnsureSuccessStatusCode(); // Ensure success or throw exception
string result = messageResponse.Content.ReadAsStringAsync().Result;
dynamic resultObj = JsonConvert.DeserializeObject<dynamic>(result);
int errorCode = resultObj.errcode;
if (errorCode != 0)
{
string errorMessage = "发送失败,错误码:" + errorCode + ", 错误信息:" + resultObj.errmsg;
LogHelper.LogError(errorMessage); // Log the error message
//throw new Exception(errorMessage);
}
}
catch (HttpRequestException httpEx)
{
string errorMessage = "HTTP请求失败:" + httpEx.Message;
LogHelper.LogError(errorMessage); // Log the error message
//throw new Exception(errorMessage);
}
catch (JsonException jsonEx)
{
string errorMessage = "JSON解析失败:" + jsonEx.Message;
LogHelper.LogError(errorMessage); // Log the error message
//throw new Exception(errorMessage);
}
catch (Exception ex)
{
string errorMessage = "发送失败:" + ex.Message;
LogHelper.LogError(errorMessage); // Log the error message
//throw new Exception(errorMessage);
}
}