溫馨提示×

溫馨提示×

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

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

C#微信開發(fā)如何實現(xiàn)接收 / 返回文本消息的方法

發(fā)布時間:2021-03-12 09:50:47 來源:億速云 閱讀:572 作者:小新 欄目:移動開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)C#微信開發(fā)如何實現(xiàn)接收 / 返回文本消息的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

接收 / 返回文本消息

①接收/返回文本消息原理說明

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

注意點:

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

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

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

POST到開發(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ù)說明:

C#微信開發(fā)如何實現(xiàn)接收 / 返回文本消息的方法

返回文本消息的XML格式:

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

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

C#微信開發(fā)如何實現(xiàn)接收 / 返回文本消息的方法

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

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

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

注意:接收消息的時候要將消息格式轉(zhuǎn)化為“GBK”格式,否則后邊進(jìn)行消息解析的時候沒辦法進(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;
}

關(guān)于“C#微信開發(fā)如何實現(xiàn)接收 / 返回文本消息的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

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

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

AI