WeChat development asp.net

高洛峰
Release: 2017-02-23 13:51:54
Original
1558 people have browsed it

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();
    }
Copy after login

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;
    }
Copy after login

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;
Copy after login

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;
    }
Copy after login
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;
    }
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template