In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use ASP.NET for Wechat development", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use ASP.NET for Wechat development" bar!
After the user of the public platform submits the information, the Wechat server will send a GET request to the entered URL with four parameters:
The developer verifies the request by verifying the signature (there is a verification method below). If it is confirmed that the GET request is from Wechat server, return the echostr parameter content as is, then the access will take effect, otherwise the access will fail.
Signature combines the token parameters entered by the developer with the timestamp parameters and nonce parameters in the request.
Encryption / verification process:
1. The three parameters token, timestamp and nonce are sorted in dictionary order.
two。 Concatenate three parameter strings into one string for sha1 encryption
3. The encrypted string obtained by the developer can be compared with signature, indicating that the request originated from Wechat
/ / verify signature / public static bool CheckSignature (String signature, String timestamp, String nonce) {String [] arr = new String [] {token, timestamp, nonce}; / / sort token, timestamp, nonce in lexicographic order Array.Sort (arr); StringBuilder content = new StringBuilder (); for (int I = 0; I < arr.Length) ITunes +) {content.Append (arr [I]);} String tmpStr = SHA1_Encrypt (content.ToString ()); / / compare the sha1 encrypted string with signature to identify that the request originated from Wechat 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 ();}
Message push after access when ordinary Wechat users send a message to the public account, the Wechat server will POST the message to the filled URL.
Protected void Page_Load (object sender, EventArgs e) {if (Request.HttpMethod.ToUpper () = = "GET") {/ / Wechat encryption signature string signature = Request.QueryString ["signature"]; / / timestamp string timestamp = Request.QueryString ["timestamp"]; / / random number string nonce = Request.QueryString ["nonce"]; / / random string 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 / / public void processRequest (String xml) {try {/ / xml request parsing Hashtable requestHT = WeixinServer.ParseXml (xml); / / sender account (open_id) string fromUserName = (string) requestHT ["FromUserName"]; / / Public account string toUserName = (string) requestHT ["ToUserName"] / / message type string msgType = (string) requestHT ["MsgType"]; / / text message 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, "Hello, I'm vinehoo,")) } else if (msgType = = ReqMsgType.Event) {/ / event type String eventType= (string) requestHT ["Event"]; / / subscribe to if (eventType==ReqEventType.Subscribe) {Response.Write (GetMainMenuMessage (toUserName, fromUserName, "Thank you for your attention! , ") } / / Unsubscribe else if (eventType==ReqEventType.Unsubscribe) {/ / TODO after unsubscribing, users can no longer receive the message sent by the official account, so there is no need to reply to the message} / / Custom menu Click event else if (eventType==ReqEventType.CLICK) {/ / TODO Custom menu right is not open Do not process this type of message}} else if (msgType = = ReqMsgType.Location) {}} catch (Exception e) {}} protected void Page_Load (object sender, EventArgs e) {if (Request.HttpMethod.ToUpper () = = "GET") {/ / Wechat encryption signature string signature = Request.QueryString ["signature"] / / timestamp string timestamp = Request.QueryString ["timestamp"]; / / random number string nonce = Request.QueryString ["nonce"]; / / random string 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 {/ / xml request parsing Hashtable requestHT = WeixinServer.ParseXml (xml); / / sender account (open_id) string fromUserName = (string) requestHT ["FromUserName"] / / Public account string toUserName = (string) requestHT ["ToUserName"]; / / message type string msgType = (string) requestHT ["MsgType"]; / / text message if (msgType = = ReqMsgType.Text) {/ / Response.Write (str); string content= (string) requestHT ["Content"]; if (content== "1") {/ / Response.Write (str); Response.Write (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, "Hello, I'm vinehoo,")) } else if (msgType = = ReqMsgType.Event) {/ / event type String eventType= (string) requestHT ["Event"]; / / subscribe to if (eventType==ReqEventType.Subscribe) {Response.Write (GetMainMenuMessage (toUserName, fromUserName, "Thank you for your attention! , ") } / / Unsubscribe else if (eventType==ReqEventType.Unsubscribe) {/ / TODO after unsubscribing, users can no longer receive the message sent by the official account, so there is no need to reply to the message} / / Custom menu Click event else if (eventType==ReqEventType.CLICK) {/ / TODO Custom menu right is not open Do not process this type of message}} else if (msgType = = ReqMsgType.Location) {}} catch (Exception e) {}}
Thank you for your reading, the above is the content of "how to use ASP.NET for Wechat development". After the study of this article, I believe you have a deeper understanding of how to use ASP.NET for Wechat development, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.