码迷,mamicode.com
首页 > 微信 > 详细

微信接口小例

时间:2017-12-12 13:45:07      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:base   empty   com   while   static   ret   msm   nba   close   

微信基础类:用于连接微信的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 数据转换

微信接口小例

标签:base   empty   com   while   static   ret   msm   nba   close   

原文地址:http://www.cnblogs.com/liusuqi/p/8027205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!