search
HomeWeChat AppletWeChat DevelopmentC# Development of WeChat Portal and Application (2)--WeChat Message Processing and Response

WeChat applications are in full swing, and many companies hope to get on the information express. This is a business opportunity and a technical direction. Therefore, it has become one of the planned arrangements to study and learn about the development of WeChat when you have time. . This series of articles hopes to comprehensively introduce the relevant development process and related experience summary of WeChat from a step-by-step perspective, hoping to give everyone an understanding of the relevant development process. This essay is mainly based on the previous article "C# Development of WeChat Portal and Application (1)--Start Using WeChat Interface" to provide an in-depth introduction and introduce the process of processing and responding to WeChat messages.

1. WeChat’s message response interaction

We know that WeChat’s server builds a bridge between the customer’s mobile phone and the developer’s server. Through the transmission and response of messages, it realizes communication with users. Interactive operation, the following is its message flow diagram.

C#开发微信门户及应用(2)--微信消息的处理和应答

The messages WeChat requests from the developer server include many types, but basically they are divided into text message processing, event message processing, voice message recognition, and The basic classification of message authentication operations before becoming a developer. Below is a message classification diagram I drew, which introduces these relationships and their respective message refinement classifications.

C#开发微信门户及应用(2)--微信消息的处理和应答

For these message requests, when we develop the server side, we need to write relevant logic for corresponding processing, and then respond to the message to the WeChat server platform.

In the previous essay, I posted the code to introduce the entry operation of WeChat message processing. The code is as follows.

        public void ProcessRequest(HttpContext context)
        {            //WHC.Framework.Commons.LogTextHelper.Info("测试记录");

            string postString = string.Empty;            
            if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
            {                using (Stream stream = HttpContext.Current.Request.InputStream)
                {
                    Byte[] postBytes = new Byte[stream.Length];
                    stream.Read(postBytes, 0, (Int32)stream.Length);
                    postString = Encoding.UTF8.GetString(postBytes);
                }                if (!string.IsNullOrEmpty(postString))
                {                    Execute(postString);
                }
            }            else
            {
                Auth();
            }
        }

Execute(postString); is the message processing function, which implements the distribution and processing of different messages. '

        /// <summary>
        /// 处理各种请求信息并应答(通过POST的请求)        /// </summary>
        /// <param>POST方式提交的数据
        private void Execute(string postStr)
        {            WeixinApiDispatch dispatch = new WeixinApiDispatch();
            string responseContent = dispatch.Execute(postStr);

            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.Write(responseContent);
        }

WeixinApiDispatch inside is a distributed management class, which extracts the content of the request message and constructs different types of message parameters , passed to different response functions for processing, and then the encapsulated XML content is returned as a response.

The specific code processing logic is shown in the figure below.

C#开发微信门户及应用(2)--微信消息的处理和应答

This message processing interface actually defines a series of processing operations for request messages. The parameters are different message objects. The specific code definition is as follows ( Due to space reasons, some interfaces are omitted, please refer to the figure above for details).

    /// <summary>
    /// 客户端请求的数据接口    /// </summary>
    public interface IWeixinAction
    {        /// <summary>
        /// 对文本请求信息进行处理        /// </summary>
        /// <param>文本信息实体
        /// <returns></returns>
        string HandleText(RequestText info);        /// <summary>
        /// 对图片请求信息进行处理        /// </summary>
        /// <param>图片信息实体
        /// <returns></returns>
        string HandleImage(RequestImage info);

...........................        /// <summary>
        /// 对订阅请求事件进行处理        /// </summary>
        /// <param>订阅请求事件信息实体
        /// <returns></returns>
        string HandleEventSubscribe(RequestEventSubscribe info);        /// <summary>
        /// 对菜单单击请求事件进行处理        /// </summary>
        /// <param>菜单单击请求事件信息实体
        /// <returns></returns>
        string HandleEventClick(RequestEventClick info);

..............................
    }

As can be seen from the above code, different messages are passed to the processing function in the form of different message entity classes (Note: The entity class is defined by myself according to the needs of program development. It is not the entity class of WeChat itself ). This is very convenient for us to handle operations. Otherwise, we need to parse different message contents each time. It is easy for problems to arise. Such strongly typed data types improve the robustness and efficiency of our development of WeChat applications. The objects of these entity classes have a certain inheritance relationship, and their inheritance relationship is as follows.

C#开发微信门户及应用(2)--微信消息的处理和应答

2. WeChat’s management interface

The above message classification is a message request operation sent by the WeChat server to the developer server. There is also a message, which is The message request or response made by our developer server to the WeChat server is temporarily called the WeChat management interface, which shows that we can perform related message replies or data management operations through these interfaces. Its classification diagram is shown below.

C#开发微信门户及应用(2)--微信消息的处理和应答

WeChat’s reply message processing is the same as the information in the above section. It is also inherited from the BaseMessage entity class (Similarly, the entity class in the figure below and Its inheritance relationship is also customized to facilitate program development), and its relationship is as follows

C#开发微信门户及应用(2)--微信消息的处理和应答

The most commonly used messages to reply to are text messages and graphic messages.

The effect of the text message is as follows.

C#开发微信门户及应用(2)--微信消息的处理和应答

Graphic messages, you can add pictures, and you can also add detailed link pages. It is a very nice effect. For some content that is relatively large, I hope to show better effects. , this is generally used, and the effect is as follows.

C#开发微信门户及应用(2)--微信消息的处理和应答

For more C# development of WeChat portals and applications (2)--WeChat message processing and response For related articles, 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function