.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值