C# Development of WeChat Portal and Application (2) Graphical Code Tutorial for WeChat Message Processing and Response

黄舟
Release: 2017-06-18 10:26:53
Original
1422 people have browsed it

The article mainly introduces the second part of C# development of WeChat portal and application in detail, WeChat message processing and response, which has certain reference value. Interested friends can refer to it

WeChat application is in full swing , 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. , realizes the interaction with the user, the following is its message flow chart.

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.

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

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


///  /// 处理各种请求信息并应答(通过POST的请求) ///  /// 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); }
Copy after login

The WeixinApiDispatch inside is a distribution management class. It extracts the content of the request message, constructs different types of message parameters, and passes them to different response functions for processing. , and then return the encapsulated XML content as a response.

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

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


///  /// 客户端请求的数据接口 ///  public interface IWeixinAction { ///  /// 对文本请求信息进行处理 ///  /// 文本信息实体 ///  string HandleText(RequestText info); ///  /// 对图片请求信息进行处理 ///  /// 图片信息实体 ///  string HandleImage(RequestImage info); ........................... ///  /// 对订阅请求事件进行处理 ///  /// 订阅请求事件信息实体 ///  string HandleEventSubscribe(RequestEventSubscribe info); ///  /// 对菜单单击请求事件进行处理 ///  /// 菜单单击请求事件信息实体 ///  string HandleEventClick(RequestEventClick info); .............................. }
Copy after login

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 me 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, different message contents need to be parsed each time, which is easy to happen. Question, such a strongly typeddata typeimproves 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.

2. WeChat 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 a message request or response from our developer server to the WeChat server. This is temporarily called WeChat's management interface, which shows that we can perform related message replies or data management operations through these interfaces. Its classification diagram is shown below.

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 It is also customized to facilitate program development), and its relationship is as follows

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

The effect of the text message is as follows.

Graphic messages, you can add pictures, and you can also add detailed link pages, which is a very good-looking effect. For those who have a lot of content and want to show better effects, this is generally used. The effect is as follows.

The above is the detailed content of C# Development of WeChat Portal and Application (2) Graphical Code Tutorial for WeChat Message Processing and Response. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!