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对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败。
signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
加密/校验流程:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
-
-
-
-
-
-
- public static bool CheckSignature(String signature, String timestamp, String nonce)
- {
- String[] arr = new String[] { 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());
-
-
-
- return tmpStr != null ? tmpStr.Equals(signature) : false;
- }
-
-
-
-
-
-
-
- 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上。
- 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);
- }
-
-
- }
-
-
-
-
-
-
- public void processRequest(String xml)
- {
- try
- {
-
-
- Hashtable requestHT = WeixinServer.ParseXml(xml);
-
-
- string fromUserName = (string)requestHT["FromUserName"];
-
- string toUserName = (string)requestHT["ToUserName"];
-
- string msgType = (string)requestHT["MsgType"];
-
-
- if (msgType == ReqMsgType.Text)
- {
-
-
- string content = (string)requestHT["Content"];
- if(content=="1")
- {
-
- 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)
- {
-
- }
-
- else if (eventType==ReqEventType.CLICK)
- {
-
- }
- }
- else if (msgType == ReqMsgType.Location)
- {
- }
-
-
- }
- catch (Exception e)
- {
-
- }
- }<pre name="code" class="csharp"> 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);
- }
-
-
- }
-
-
-
-
-
-
- public void processRequest(String xml)
- {
- try
- {
-
-
- Hashtable requestHT = WeixinServer.ParseXml(xml);
-
-
- string fromUserName = (string)requestHT["FromUserName"];
-
- string toUserName = (string)requestHT["ToUserName"];
-
- string msgType = (string)requestHT["MsgType"];
-
-
- if (msgType == ReqMsgType.Text)
- {
-
-
- string content = (string)requestHT["Content"];
- if(content=="1")
- {
-
- 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)
- {
-
- }
-
- else if (eventType==ReqEventType.CLICK)
- {
-
- }
- }
- else if (msgType == ReqMsgType.Location)
- {
- }
-
-
- }
- catch (Exception e)
- {
-
- }
- }</pre><br>
- <pre></pre>
- <br>
- <br>
asp.net微信公众平台开发
原文:http://www.cnblogs.com/Alex80/p/4236062.html