• 微信接口小例


    微信基础类:用于连接微信的API,获取或发送数据。

     1  static class WeiXinBase
     2     {
     3         public static string GetAccessToken(WeiXinConfig config)
     4         {
     5             string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + config.AppID + "&secret=" + config.AppSecret;
     6             JMAccessToken at = JsonHelper.ParseFormByJson<JMAccessToken>(HttpGet(url));
     7             return at.access_token;
     8         }
     9 
    10         public static string HttpGet(string url)
    11         {
    12             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    13             request.Method = "GET";
    14             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    15             Stream responseStream = response.GetResponseStream();
    16             string StrDate = string.Empty;
    17             string strValue = string.Empty;
    18             StreamReader Reader = new StreamReader(responseStream, Encoding.UTF8);
    19             while ((StrDate = Reader.ReadLine()) != null)
    20             {
    21                 strValue += StrDate + "
    ";
    22             }
    23             responseStream.Close();
    24             return strValue;
    25         }
    26 
    27         public static string HttpPost(string url, string postData)
    28         {
    29             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    30             request.Method = "POST";
    31             request.ContentType = "application/json;charset=UTF-8";
    32             byte[] payload = System.Text.Encoding.UTF8.GetBytes(postData);
    33             request.ContentLength = payload.Length;
    34             Stream requestStream = request.GetRequestStream();
    35             requestStream.Write(payload, 0, payload.Length);
    36             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    37             Stream responseStream = response.GetResponseStream();
    38             string StrDate = string.Empty;
    39             string strValue = string.Empty;
    40             StreamReader Reader = new StreamReader(responseStream, Encoding.UTF8);
    41             while ((StrDate = Reader.ReadLine()) != null)
    42             {
    43                 strValue += StrDate + "
    ";
    44             }
    45             requestStream.Close();
    46             responseStream.Close();
    47             return strValue;
    48         }
    49     }
    50  public class WeiXinConfig
    51     {
    52         public string AppID { get; set; }
    53         public string AppSecret { get; set; }
    54         public string AccessToken { get; set; }
    55     }
    56 public class JMAccessToken
    57     {
    58         public string access_token { get; set; }
    59         public string expires_in { get; set; }
    60     }

    发消息功能

     1 public class WeixinSms
     2     {
     3         private WeiXinConfig wxConfig;
     4 
     5         public WeixinSms(WeiXinConfig config)
     6         {
     7             this.wxConfig = config;
     8         }
     9 
    10         public string Send(JMSendSms jmsms)
    11         {
    12             string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + WeiXinBase.GetAccessToken(this.wxConfig);
    13             return WeiXinBase.HttpPost(url, JsonHelper.ToJson<JMSendSmsAll>(jmsms));
    14         }
    15 
    16         public string SendAll(JMSendSmsAll jmall)
    17         {
    18             string url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + WeiXinBase.GetAccessToken(this.wxConfig);
    19             return WeiXinBase.HttpPost(url, JsonHelper.ToJson<JMSendSmsAll>(jmall));
    20         }
    21     }

    Json数据转换请参考: JSON 数据转换

  • 相关阅读:
    MySQL-后知知觉的索引
    MySQL运行状态show status详解
    在Linux系统中,一个文件的访问权限是755,其含义是什么?
    wlst的应用
    Linux中管理硬盘设备和内存
    Linux中挂载mount挂载命令
    Linux的文件系统与数据资料
    Linux中远程传输命令
    Weblogic中wlst的使用
    Weblogic中,如果管理服务器失效了,受管服务器也可以启用,而不影响受管服务器的实例
  • 原文地址:https://www.cnblogs.com/liusuqi/p/8027205.html
Copyright © 2020-2023  润新知