.net 后台以post方式调用微信公众平台接口

1 public class Fresult
2 {
3         public int errcode { get; set; }
4         public string errmsg { get; set; }
5         public string msgid { get; set; }
6 }
 1  public static Fresult SendTemplateMessage(string accessToken, string body)
 2 {
 3             Fresult fresult = new Fresult();
 4             string uriStr = $"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={accessToken}";
 5             var uri = new Uri(uriStr);
 6             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
 7             request.Method = "POST";
 8             request.ContentType = "application/json";
 9             request.Accept = "application/json";
10             Encoding encoding = Encoding.UTF8;
11             byte[] data = encoding.GetBytes(body);
12             Stream sm = request.GetRequestStream();
13             sm.Write(data, 0, data.Length);
14             sm.Close();
15             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
16             using (Stream streamResponse = response.GetResponseStream())
17             {
18                 using (StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8))
19                 {
20                     char[] readBuff = new char[256];
21                     int count = streamRead.Read(readBuff, 0, 256);
22                     string content = "";
23                     while (count > 0)
24                     {
25                         string outputData = new string(readBuff, 0, count);
26                         content += outputData;                                             
27                         count = streamRead.Read(readBuff, 0, 256);
28                     }
29                     fresult = JsonConvert.DeserializeObject<Fresult>(content);
30                 }
31             }
32             response.Close();
33             response.Dispose();
34             return fresult;
35 }
原文地址:https://www.cnblogs.com/jasonbourne3/p/11098209.html