微信接口小例

Posted M守护神.NET

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信接口小例相关的知识,希望对你有一定的参考价值。

微信基础类:用于连接微信的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 + "\\r\\n";
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 + "\\r\\n";
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 数据转换

以上是关于微信接口小例的主要内容,如果未能解决你的问题,请参考以下文章

代码生成器小例

[aspx]控件及代码小例

微信小程序代码片段分享

微信小程序入门-指南针

android 小例之两列菜单关联

使用libcurl下载文件小例