C # WeChat Development Series (4)-receiving/returning text messages,

Source: Internet
Author: User

C # Development Series (4)-receiving/returning text messages,
4.0 receive/return text messages

 

① Principles of receiving/returning text messages

 

When a common user sends a message to a public account, the server sends the XML data packet of the POST message to the URL entered by the developer. Before proceeding with development, read the common message receiving development document on the public platform, have a certain understanding of this message processing mechanism before proceeding with development (Development and reception of common message development documents)

Note:

1. msgid is recommended for message deduplication of retries.

2. If the server fails to receive the response within five seconds, the connection will be disconnected and a request will be initiated again. A total of three retries will be made. If the server cannot process and reply within five seconds, it can directly reply to an empty string. The server will not process the request and will not initiate a retry. For details, see "Send message-passive reply message ".

3. To ensure higher security, developers can set message encryption at the Developer Center on the official website of the public platform. After encryption is enabled, messages sent by users are encrypted, and messages sent by users passively replying to users by public accounts need to be encrypted. (However, developers send messages to users through APIS such as customer service interfaces, ). For more information about message encryption and decryption, see message encryption and decryption instructions ".

The XML format of POST to the developer server is:

 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>  <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> <MsgId>1234567890123456</MsgId> </xml>

Parameters for receiving message packets:

 

XML format of the returned text message:

<Xml> <ToUserName> <! [CDATA [toUser]> </ToUserName> <FromUserName> <! [CDATA [fromUser]> </FromUserName> <CreateTime> 12345678 </CreateTime> <MsgType> <! [CDATA [text]> </MsgType> <Content> <! [CDATA [Hello]> </Content> </xml>

 

Parameter descriptions of returned text message packets:

 

② Code implementation for receiving/returning text messages

 

The XML data packet receiving code sent from the server POST is as follows:

1 if (IsPostBack) 2 {3 // ********************************* automatic response code block ********************************* 4 string postString = string. empty; 5 using (Stream stream = HttpContext. current. request. inputStream) 6 {7 Byte [] postBytes = new Byte [stream. length]; 8 stream. read (postBytes, 0, (Int32) stream. length); 9 // The received message is in GBK format 10 postString = Encoding. getEncoding ("GBK "). getString (postBytes); 11 string responseContent = help. returnMessage (postString); 12 // The returned message is in UTF-8 format 13 HttpContext. current. response. contentEncoding = Encoding. UTF8; 14 HttpContext. current. response. write (responseContent ); 15} 16 // ******************************** automatically reply to the code block end ******************************** 17}

Note:When receiving a message, you must convert the Message format to "GBK". Otherwise, the message cannot be effectively parsed when parsing the message.

The ReturnMessage () processing method code is as follows:

1 /// <summary> 2 // unified global response message processing method 3 /// </summary> 4 /// <param name = "postStr"> </param> 5 // <returns> </returns> 6 public string ReturnMessage (string postStr) 7 {8 string responseContent = ""; 9 XmlDocument xmldoc = new XmlDocument (); 10 xmldoc. load (new System. IO. memoryStream (System. text. encoding. getEncoding ("GB2312 "). getBytes (postStr); 11 XmlNode MsgType = xmldoc. selectSingleNode ("/xml/MsgType" ); 12 if (MsgType! = Null) 13 {14 switch (MsgType. innerText) 15 {16 case "event": 17 responseContent = EventHandle (xmldoc); // menu event processing 18 break; 19 case "text": 20 responseContent = TextHandle (xmldoc ); // text message processing 21 break; 22 default: 23 break; 24} 25} 26 return responseContent; 27}

The code for processing TextHandle (xmldoc) is as follows:

1 /// <summary> 2 // accept text messages and reply to custom messages 3 /// </summary> 4 /// <param name = "xmldoc"> </param> 5 // <returns> </returns> 6 public string TextHandle (XmlDocument xmldoc) 7 {8 string responseContent = ""; 9 XmlNode ToUserName = xmldoc. selectSingleNode ("/xml/ToUserName"); 10 XmlNode FromUserName = xmldoc. selectSingleNode ("/xml/FromUserName"); 11 XmlNode Content = xmldoc. selectSingleNode ("/xml/Content"); 12 if (Content! = Null) 13 {14 if (Content. innerText = "specify the custom text for the reply message") 15 {16 responseContent = string. format (XMLTemplate. message_Text, 17 FromUserName. innerText, 18 ToUserName. innerText, 19 DateTime. now. ticks, 20 "Custom reply message content"); 21} 22} 23 return responseContent; 24}

The Code demonstration for implementing the function has been completed, and other message processing modes are also used for interaction, such: the preceding code can be used to receive/reply text messages, image messages, voice messages, video messages, small video messages, geographic location messages, and link messages.

 

If you are interested in the Series C # Development Series, you can follow my other articles (continuously updating:

C # Development Series (Top)-complete learning path for development

C # Development Series (4)-receiving/returning text messages

C # Development Series (3)-getting interface call creden》

C # development series (2)-custom menu management

C # Development Series (1)-enable developer Mode

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.