zoukankan      html  css  js  c++  java
  • .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 }
  • 相关阅读:
    ajax
    Django之modelform组件
    Django之form组件
    orm事务与锁
    orm之多表操作
    orm之单表操作
    Django之orm
    Django之模板系统
    Django之视图
    hdu5698瞬间移动(杨辉三角+快速幂+逆元)
  • 原文地址:https://www.cnblogs.com/jasonbourne3/p/11098209.html
Copyright © 2011-2022 走看看