asp.net微信公众平台开发
2014-08-23 18:15
211 查看
http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97
微信公众平台接口指南
微信公众平台的开发比较简单,首先是网址接入
公众平台用户提交信息后,微信服务器将发送GET请求到填写的URL上,并且带上四个参数:
参数 | 描述 |
---|---|
signature | 微信加密签名 |
timestamp | 时间戳 |
nonce | 随机数 |
echostr | 随机字符串 |
signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
加密/校验流程: 1. 将token、timestamp、nonce三个参数进行字典序排序 2. 将三个参数字符串拼接成一个字符串进行sha1加密 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信 <pre class="csharp" name="code"> /// <summary> /// 验证签名 /// </summary> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <returns></returns> public static bool CheckSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { token, timestamp, nonce }; // 将token、timestamp、nonce三个参数进行字典序排序 Array.Sort<String>(arr); StringBuilder content = new StringBuilder(); for (int i = 0; i < arr.Length; i++) { content.Append(arr[i]); } String tmpStr = SHA1_Encrypt(content.ToString()); // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信 return tmpStr != null ? tmpStr.Equals(signature) : false; } /// <summary> /// 使用缺省密钥给字符串加密 /// </summary> /// <param name="Source_String"></param> /// <returns></returns> public static string SHA1_Encrypt(string Source_String) { byte[] StrRes = Encoding.Default.GetBytes(Source_String); HashAlgorithm iSHA = new SHA1CryptoServiceProvider(); StrRes = iSHA.ComputeHash(StrRes); StringBuilder EnText = new StringBuilder(); foreach (byte iByte in StrRes) { EnText.AppendFormat("{0:x2}", iByte); } return EnText.ToString(); }
接入后是消息推送当普通微信用户向公众账号发消息时,微信服务器将POST该消息到填写的URL上。
<pre class="html" name="code"> protected void Page_Load(object sender, EventArgs e) { if (Request.HttpMethod.ToUpper() == "GET") { // 微信加密签名 string signature = Request.QueryString["signature"]; // 时间戳 string timestamp = Request.QueryString["timestamp"]; // 随机数 string nonce = Request.QueryString["nonce"]; // 随机字符串 string echostr = Request.QueryString["echostr"]; if (WeixinServer.CheckSignature(signature, timestamp, nonce)) { Response.Write(echostr); } } else if (Request.HttpMethod.ToUpper() == "POST") { StreamReader stream = new StreamReader(Request.InputStream); string xml = stream.ReadToEnd(); processRequest(xml); } } /// <summary> /// 处理微信发来的请求 /// </summary> /// <param name="xml"></param> public void processRequest(String xml) { try { // xml请求解析 Hashtable requestHT = WeixinServer.ParseXml(xml); // 发送方帐号(open_id) string fromUserName = (string)requestHT["FromUserName"]; // 公众帐号 string toUserName = (string)requestHT["ToUserName"]; // 消息类型 string msgType = (string)requestHT["MsgType"]; //文字消息 if (msgType == ReqMsgType.Text) { // Response.Write(str); string content = (string)requestHT["Content"]; if(content=="1") { // Response.Write(str); Response.Write(GetNewsMessage(toUserName, fromUserName)); return; } if (content == "2") { Response.Write(GetUserBlogMessage(toUserName, fromUserName)); return; } if (content == "3") { Response.Write(GetGroupMessage(toUserName, fromUserName)); return; } if (content == "4") { Response.Write(GetWinePartyMessage(toUserName, fromUserName)); return; } Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); } else if (msgType == ReqMsgType.Event) { // 事件类型 String eventType = (string)requestHT["Event"]; // 订阅 if (eventType==ReqEventType.Subscribe) { Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!,")); } // 取消订阅 else if (eventType==ReqEventType.Unsubscribe) { // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息 } // 自定义菜单点击事件 else if (eventType==ReqEventType.CLICK) { // TODO 自定义菜单权没有开放,暂不处理该类消息 } } else if (msgType == ReqMsgType.Location) { } } catch (Exception e) { } }<PRE class=csharp name="code"> protected void Page_Load(object sender, EventArgs e) { if (Request.HttpMethod.ToUpper() == "GET") { // 微信加密签名 string signature = Request.QueryString["signature"]; // 时间戳 string timestamp = Request.QueryString["timestamp"]; // 随机数 string nonce = Request.QueryString["nonce"]; // 随机字符串 string echostr = Request.QueryString["echostr"]; if (WeixinServer.CheckSignature(signature, timestamp, nonce)) { Response.Write(echostr); } } else if (Request.HttpMethod.ToUpper() == "POST") { StreamReader stream = new StreamReader(Request.InputStream); string xml = stream.ReadToEnd(); processRequest(xml); } } /// <summary> /// 处理微信发来的请求 /// </summary> /// <param name="xml"></param> public void processRequest(String xml) { try { // xml请求解析 Hashtable requestHT = WeixinServer.ParseXml(xml); // 发送方帐号(open_id) string fromUserName = (string)requestHT["FromUserName"]; // 公众帐号 string toUserName = (string)requestHT["ToUserName"]; // 消息类型 string msgType = (string)requestHT["MsgType"]; //文字消息 if (msgType == ReqMsgType.Text) { // Response.Write(str); string content = (string)requestHT["Content"]; if(content=="1") { // Response.Write(str); Response.Write(GetNewsMessage(toUserName, fromUserName)); return; } if (content == "2") { Response.Write(GetUserBlogMessage(toUserName, fromUserName)); return; } if (content == "3") { Response.Write(GetGroupMessage(toUserName, fromUserName)); return; } if (content == "4") { Response.Write(GetWinePartyMessage(toUserName, fromUserName)); return; } Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); } else if (msgType == ReqMsgType.Event) { // 事件类型 String eventType = (string)requestHT["Event"]; // 订阅 if (eventType==ReqEventType.Subscribe) { Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!,")); } // 取消订阅 else if (eventType==ReqEventType.Unsubscribe) { // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息 } // 自定义菜单点击事件 else if (eventType==ReqEventType.CLICK) { // TODO 自定义菜单权没有开放,暂不处理该类消息 } } else if (msgType == ReqMsgType.Location) { } } catch (Exception e) { } }</PRE><BR> <PRE></PRE> <BR> <BR>
相关文章推荐
- [c#]asp.net开发微信公众平台(5)微信图文消息
- asp.net微信公众平台开发(3)微信消息封装及反射赋值
- asp.net开发微信公众平台之验证消息的真实性
- 微信公众平台开发小记(ASP.NET)
- asp.net开发微信公众平台之获取用户消息并处理
- [c#]asp.net开发微信公众平台(3)微信消息封装及反射赋值
- [c#]asp.net开发微信公众平台(1)数据库设计
- ASP.NET Web API实现微信公众平台开发(三)自定义菜单
- 沐雪多用户微信公众平台开发源码(2016年最新的asp.net C# 微信源码)
- 自动回复消息-微信公众平台开发4(asp.net)
- token验证-微信公众平台开发3(asp.net)
- [c#]asp.net开发微信公众平台(8)微信9大高级接口,自定义菜单
- ASP.NET 微信公众平台模板消息推送功能完整开发
- asp.net开发微信公众平台之验证消息的真实性
- [c#]asp.net开发微信公众平台(2)多层架构框架搭建和入口实现
- [c#]asp.net开发微信公众平台(4)关注事件、用户记录、回复文本消息
- asp.net微信公众平台开发
- asp.net 微信公众平台开发(一)公众账号的注册、设置、登录
- C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台
- ASP.NET Web API实现微信公众平台开发(一)服务器验证