Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Wechat developing asp.net

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "Wechat development asp.net sample analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and study "Wechat development asp.net sample analysis" this article.

Recently, I came into contact with Wechat development, and I was also looking at the code of php, but finally I used the c # language.

A new index.ashx file has been created in the background, which is faster.

First of all, the top quotes

Using System.IO

Using System.Xml

One is to receive the xml file stream, and the other is to process the xml file later.

Public class index: IHttpHandler {private readonly string Token = "xxxx"; / / consistent with the Token setting of the Wechat public account backend, and case-sensitive. Public void ProcessRequest (HttpContext context) {context.Response.ContentType = "text/plain"; string signature = context.Request ["signature"]; string timestamp = context.Request ["timestamp"]; string nonce = context.Request ["nonce"]; string echostr = context.Request ["echostr"] If (context.Request.HttpMethod = = "GET") {if (CheckSign (signature, timestamp, nonce)) {context.Response.Output.Write (echostr) }} else {/ / post method-triggered when a user wants to send a message to a public account, write the event} context.Response.End ();}

First of all, set up the Token, receive various parameters, and send the request in the way of get.

Here is mainly the CheckSign () function.

Public bool CheckSign (string signature, string timestamp, string nonce) {string [] strs = new string [] {Token, timestamp, nonce}; Array.Sort (strs); / sort string strNew = string.Join ("", strs); / / concatenate the string strNew = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (strNew, "SHA1"); / / encrypt if (signature = strNew.ToLower () return true) Return false;}

In fact, the consciousness here is to receive A _ B _ C _ D _ D _ E for customization, and B/C/E to generate F, which, compared with A, returns the output D equally.

String xmlFromWeChat = new StreamReader (context.Request.InputStream). ReadToEnd (); / / read XML stream XmlDocument xmldocument = new XmlDocument (); xmldocument.LoadXml (xmlFromWeChat); load the string string fromContent = xmldocument.GetElementsByTagName ("Content"). Item (0) .InnerText; string fromMsgType = xmldocument.GetElementsByTagName ("MsgType"). Item (0). InnerText

The writing is not good to point out!

In this way, we can judge the received data and make corresponding operations. The most important thing is to be familiar with the interface.

Here is an example that may not be abstract:

Public string receiveText (string xmlFromWeChat) {XmlDocument xmlText = new XmlDocument (); xmlText.LoadXml (xmlFromWeChat); string content; string xmlStr; string keyword = xmlText.GetElementsByTagName ("Content"). Item (0). InnerText.Trim (); content = "Welcome to xxx!" String [] defArray = {xmlText.GetElementsByTagName ("FromUserName"). Item (0). InnerText, xmlText.GetElementsByTagName ("ToUserName"). Item (0). InnerText, ConvertDateTimeInt (DateTime.Now). ToString (), content}; xmlStr = transmitText (defArray) } return xmlStr } public string transmitText (string [] xmlArray) {string xmlstring = @ "{2}"; string xmlstr = string.Format (xmlstring, xmlArray) Return xmlstr;}

This is the completion of a simple reply.

The above is all the content of this article "sample Analysis of Wechat Development asp.net". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report