Recently, I am engaged in development, and I am also reading php code, but I still use the c # language. recently I am engaged in development, and I am also reading php code, however, I still use the c # language;
The index. ashx file is created in the background, which is faster;
First, the top references
Using System. IO;
Using System. Xml;
One is to receive xml file streams, and the other is to process xml files later;
Public class index: IHttpHandler {private readonly string Token = "xxxx"; // it is case sensitive and consistent with the Token settings in the public account background. 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-when a user wants a public account to send a message, Write the Event} context. response. end ();}
First, set the Token to receive various parameters. the request is sent in get mode;
Here is 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); // connect to 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, E is custom, B/C/E generates F, and compare with A, returns the output D;
String xmlFromWeChat = new StreamReader (context. request. inputStream ). readToEnd (); // read the XML stream XmlDocument xmldocument = new XmlDocument (); xmldocument. loadXml (xmlFromWeChat); load string fromContent = xmldocument. getElementsByTagName ("Content "). item (0 ). innerText; string fromMsgType = xmldocument. getElementsByTagName ("MsgType "). item (0 ). innerText;
It's hard to write !!
In this way, we can judge the received data and perform corresponding operations. The most important thing is to be familiar with interfaces;
The following example shows that there may be no good abstraction:
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 = @"
{0}
{1}
{2}
text
{3}
"; string xmlstr = string.Format(xmlstring, xmlArray); return xmlstr; }
In this way, a simple reply is completed;
For more articles related to asp.net development, please follow PHP Chinese network!