Home  >  Article  >  WeChat Applet  >  WeChat development asp.net

WeChat development asp.net

高洛峰
高洛峰Original
2017-02-23 13:51:541554browse

I have recently been exposed to WeChat development, and I have also been looking at PHP code, but in the end I still used c# language;

I created a new index.ashx file in the background, which is faster;

First of all The top quote

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";//与微信公众账号后台的Token设置保持一致,区分大小写。
    
    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 - 当有用户想公众账号发送消息时触发,写事件
        }

        context.Response.End();
    }

First set up the Token and receive various parameters. The request method is sent in the get method;

The main thing here is the CheckSign() function;

public bool CheckSign(string signature, string timestamp, string nonce)
    {
        string[] strs = new string[] { Token, timestamp, nonce };
        Array.Sort(strs);//排序
        string strNew = string.Join("", strs);//连接成字符串
        strNew = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strNew, "SHA1");//加密
        if (signature == strNew.ToLower())
            return true;
        return false;
    }

In fact, the awareness here It is to receive A/B/C/D, E is customized, B/C/E generates F, compare it with A, if equal, return the output D;

string xmlFromWeChat = new StreamReader(context.Request.InputStream).ReadToEnd();//读取XML流
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.LoadXml(xmlFromWeChat);加载字符串
            string fromContent = xmldocument.GetElementsByTagName("Content").Item(0).InnerText;
            string fromMsgType = xmldocument.GetElementsByTagName("MsgType").Item(0).InnerText;

is not well written Point it 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 to illustrate , maybe not very 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 = "欢迎关注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 = @"<xml>
                            <tousername></tousername>
                            <fromusername></fromusername>
                            <createtime>{2}</createtime>
                            <msgtype></msgtype>
                            <content></content>
                            </xml>";
        string xmlstr = string.Format(xmlstring, xmlArray);
        return xmlstr;
    }

This is a simple reply;

WeChat development asp.net


For more articles related to WeChat development asp.net, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn