溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中怎么接收和返回文本消息

發(fā)布時(shí)間:2021-07-08 15:43:25 來(lái)源:億速云 閱讀:170 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

今天就跟大家聊聊有關(guān)C#中怎么接收和返回文本消息,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

4.0接收 / 返回文本消息

①接收/返回文本消息原理說(shuō)明

當(dāng)普通微信用戶向公眾賬號(hào)發(fā)消息時(shí),微信服務(wù)器將POST消息的XML數(shù)據(jù)包到開(kāi)發(fā)者填寫的URL上,著手開(kāi)發(fā)之前先行閱讀微信公眾平臺(tái)接收普通消息微信開(kāi)發(fā)文檔,對(duì)微信的這種消息處理機(jī)制有一定了解之后再著手開(kāi)發(fā)(微信開(kāi)發(fā)接收普通消息開(kāi)發(fā)文檔)

注意點(diǎn):

1、關(guān)于重試的消息排重,推薦使用msgid排重。

2、微信服務(wù)器在五秒內(nèi)收不到響應(yīng)會(huì)斷掉連接,并且重新發(fā)起請(qǐng)求,總共重試三次。假如服務(wù)器無(wú)法保證在五秒內(nèi)處理并回復(fù),可以直接回復(fù)空串,微信服務(wù)器不會(huì)對(duì)此作任何處理,并且不會(huì)發(fā)起重試。詳情請(qǐng)見(jiàn)“發(fā)送消息-被動(dòng)回復(fù)消息”。

3、為了保證更高的安全保障,開(kāi)發(fā)者可以在公眾平臺(tái)官網(wǎng)的開(kāi)發(fā)者中心處設(shè)置消息加密。開(kāi)啟加密后,用戶發(fā)來(lái)的消息會(huì)被加密,公眾號(hào)被動(dòng)回復(fù)用戶的消息也需要加密(但開(kāi)發(fā)者通過(guò)客服接口等API調(diào)用形式向用戶發(fā)送消息,則不受影響)。關(guān)于消息加解密的詳細(xì)說(shuō)明,請(qǐng)見(jiàn)“消息加解密說(shuō)明”。

POST到開(kāi)發(fā)者服務(wù)器上邊的XML格式為:

 <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>

接收消息數(shù)據(jù)包參數(shù)說(shuō)明:

C#中怎么接收和返回文本消息

返回文本消息的XML格式:

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

返回文本消息數(shù)據(jù)包參數(shù)說(shuō)明:

C#中怎么接收和返回文本消息

②接收/返回文本消息代碼實(shí)現(xiàn)

開(kāi)發(fā)者在自己服務(wù)器上邊接收微信服務(wù)器POST過(guò)來(lái)的XML數(shù)據(jù)包接收代碼如下:

if(IsPostBack)
{ 
  //*********************************自動(dòng)應(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);
  }
  //********************************自動(dòng)應(yīng)答代碼塊end*******************************
}

注意:接收消息的時(shí)候要將消息格式轉(zhuǎn)化為“GBK”格式,否則后邊進(jìn)行消息解析的時(shí)候沒(méi)辦法進(jìn)行有效解析。

ReturnMessage()處理方法代碼如下:

/// <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)處理方法代碼如下:

/// <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;
}

看完上述內(nèi)容,你們對(duì)C#中怎么接收和返回文本消息有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI