C # developing WeChat Portal and its application (II.) The text code tutorial of WeChat message processing and response

Source: Internet
Author: User
The article mainly introduces the C # development portal and application of the second, message processing and response, with a certain reference value, interested in small partners can refer to

Application in full swing, many companies want to catch Information Express, this is a business opportunity, is also a technical direction, therefore, the research under the study of the relevant development, also become one of the planning things. This series of articles hope that from a gradual point of view, a comprehensive introduction of the relevant development process and related experience summarized, I hope to give you a look at the relevant development history. This essay is based on the previous article "C # Development Portal and Application (1)--Start using interface" on the basis of an in-depth introduction, the process of message processing and response.

1, the message reply interaction

We know that the server is a bridge between the customer's mobile phone and the developer server, through the message delivery and response, to achieve the interaction with the user, the following is its message flowchart.

The message to the developer server contains a variety of types, but basically, it is divided into text message processing, event message processing, voice message recognition, and before becoming a developer of the message authentication Operation basic classification, here is a message I draw a classification diagram, which describes these relationships, and the individual message refinement classifications.

In the case of requests for these messages, we need to write the relevant logic for the corresponding processing on the development server side, and then give the server platform a response message.

In the previous essay, I posted the code that describes the entry operation for message processing, as shown in the code below.


  public void ProcessRequest (HttpContext context)  {   //whc.framework.commons.logtexthelper.info ("Test Record");   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 ();   }  }

One of the Execute (poststring) is the processing function of the message, which implements the distribution process of the different messages. ‘


    <summary>    ///processing various request information and answering (request via post)///</summary>//    <param name= "Poststr" > Post method submitted data </param>    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);    }

The Weixinapidispatch inside is a distributed management class that extracts the contents of the request message, constructs different types of message parameters, passes it to different response functions for processing, and then returns the encapsulated XML content as a response.

The specific code processing logic is as shown.

This message processing interface, in fact, is to define a series of processing operations on the request message, the parameters are different to the message object, the specific code definition is as follows (due to space reasons, omit some of the interface, specifically can refer to).


  <summary>///client-requested data interface///</summary> public interface Iweixinaction {//<summary>/ Processing of text request information//</summary>//<param name= "Info" > Text information entity </param>//<returns></ret    Urns> string Handletext (requesttext info);  <summary>////Processing picture request information///</summary>/<param name= "info" > Picture information entity </param>// <returns></returns> string handleimage (requestimage info), ....... ...///&LT;SUMMARY&G.//////    T Processing of Subscription request events//</summary>//<param name= "info" > subscription request event Information entity </param>//&LT;RETURNS&GT;&LT    ;/returns> string Handleeventsubscribe (requesteventsubscribe info); <summary>////Menu click Request Event Processing///</summary>/<param name= "Info" > menu click Request Event Information Entity </param&gt    ;  <returns></returns> string Handleeventclick (Requesteventclick info), .................... }

As can be seen from the above code, different messages, to the processing function here, in a different message entity class way passed over ( Note: The entity class is I need to define the program development, not its own entity class ), so it is very convenient for us to handle the operation, Otherwise, each time you need to parse different message content, it is easy to have problems, such a strong type of data type, improve our development of the application of strong and efficient. The objects of these entity classes have a certain inheritance relationship, and their inheritance is shown below.

2, the Management interface

The message classification above is the message request operation sent by the server to the developer server, and the message is the message request or response from our developer server to the server, this is what we call the management interface, which shows that we can do the related message reply or data management operation through these interfaces. Its classification diagram is shown below.

Reply message processing, it is also the same as the information in the above section, it is also inherited from the Basemessage entity class (the same, the entity class and its inheritance is also customized to facilitate the development of the program), its relationship is as follows

Replies to the message, the most commonly used are text messages and graphics messages.

The effect of the text message is as follows.

The text message, can increase the picture, also may add the detailed link page, is very good-looking effect, for some content more, hoped to show the better effect, generally uses this, the effect is as follows.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.