亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home WeChat Applet WeChat Development C# WeChat development principle and implementation method of receiving/returning text messages

C# WeChat development principle and implementation method of receiving/returning text messages

Mar 16, 2017 pm 02:52 PM

This articleC#The principle and implementation method of receiving/returning text messages in WeChat development has certain reference value. Let’s take a look at it with the editor

Receive/ Returning text messages

①Instructions on the principle of receiving/returning text messages

When ordinary WeChat users send messages to public accounts, WeChat The server sends the XML data packet of the POST message to the URL filled in by the developer. Before starting development, read the WeChat development document for WeChat public platform to receive ordinary messages, and have a certain understanding of WeChat's message processing mechanism. Start development (WeChat development document for receiving ordinary messages)

Notes:

1. About retrying For message duplication, it is recommended to use msgid to deduplicate messages.

#2. If the WeChat server does not receive a response within five seconds, it will disconnect the connection and re-initiate the request, retrying three times in total. If the server cannot guarantee to process and reply within five seconds, you can directly reply with an empty string. The WeChat server will not do anything with this and will not initiate a retry. For details, please see "Send Message-Passive Reply Message".

#3. In order to ensure higher security, developers can set message encryption in the developer center on the official website of the public platform. After encryption is turned on, messages sent by users will be encrypted, and public accounts’ passive replies to users’ messages also need to be encrypted (but developers send messages to users through customer service Interface and other API calls) , will not be affected). For detailed instructions on message encryption and decryption, please see "Message Encryption and Decryption Instructions".

The XML format for 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>

Receive messagePacket parameter description:

C#微信開發(fā)之接收 / 返回文本消息原理與實現(xiàn)方法

## Returns the XML format of the text message:

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

Return text message packet parameter description:

C#微信開發(fā)之接收 / 返回文本消息原理與實現(xiàn)方法

##②Receive/ Return text message code implementationThe developer receives the XML data packet POST from the WeChat server on his own server. The receiving code is as follows:

if(IsPostBack)
{ 
  //*********************************自動應(yīng)答代碼塊*********************************
  string postString = string.Empty;
  using (Stream stream = HttpContext.Current.Request.InputStream)
  {
    Byte[] postBytes = new Byte[stream.Length];
    stream.Read(postBytes, 0, (Int32)stream.Length);
    //接收的消息為GBK格式
    postString = Encoding.GetEncoding("GBK").GetString(postBytes);
    string responseContent = help.ReturnMessage(postString );
    //返回的消息為UTF-8格式
    HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
    HttpContext.Current.Response.Write(responseContent);
  }
  //********************************自動應(yīng)答代碼塊end*******************************
}

Note: When receiving a message, the message format must be converted into "GBK" format, otherwise it will not be effectively parsed when parsing the message later.

ReturnMessage() processing method code is as follows:

/// <summary>
/// 統(tǒng)一全局返回消息處理方法
/// </summary>
/// <param name="postStr"></param>
/// <returns></returns>
public string ReturnMessage(string postStr)
{
  string responseContent = "";
  XmlDocument xmldoc = new XmlDocument();
  xmldoc.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("GB2312").GetBytes(postStr)));
  XmlNode MsgType = xmldoc.SelectSingleNode("/xml/MsgType");
  if (MsgType != null)
  {
    switch (MsgType.InnerText)
    {
      case "event":
        responseContent = EventHandle(xmldoc);//菜單事件處理
        break;
      case "text":
        responseContent = TextHandle(xmldoc);//文本消息處理
        break;
      default:
        break;
   }
  }
  return responseContent;
}

TextHandle(xmldoc) processing method code is as follows:

 /// <summary>
/// 接受文本消息并回復(fù)自定義消息
/// </summary>
/// <param name="xmldoc"></param>
/// <returns></returns>
public string TextHandle(XmlDocument xmldoc)
{
 string responseContent = "";
 XmlNode ToUserName = xmldoc.SelectSingleNode("/xml/ToUserName");
 XmlNode FromUserName = xmldoc.SelectSingleNode("/xml/FromUserName");
 XmlNode Content = xmldoc.SelectSingleNode("/xml/Content");
 if (Content != null)
 {
   if (Content.InnerText == "指定回復(fù)消息的自定義文本")
   {
     responseContent = string.Format(XMLTemplate.Message_Text,
       FromUserName.InnerText,
       ToUserName.InnerText,
       DateTime.Now.Ticks,
       "自定義回復(fù)消息內(nèi)容");
   }
 }
 return responseContent;
}

The code demonstration to implement the function here has been completed, and the rest will follow The message processing mode is also based on this method for interaction, such as: receiving/replying text messages,

Picture

messages, voice messages, Video messages, short video messages, geographical location messages, Link messages, etc. can be implemented by referring to the above code.

The above is the detailed content of C# WeChat development principle and implementation method of receiving/returning text messages. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72