程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 《C#微信開發系列(4)-接收 / 返回文本消息》,

《C#微信開發系列(4)-接收 / 返回文本消息》,

編輯:C#入門知識

《C#微信開發系列(4)-接收 / 返回文本消息》,


4.0接收 / 返回文本消息

 

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

 

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

注意點:

1、關於重試的消息排重,推薦使用msgid排重。

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

3、為了保證更高的安全保障,開發者可以在公眾平台官網的開發者中心處設置消息加密。開啟加密後,用戶發來的消息會被加密,公眾號被動回復用戶的消息也需要加密(但開發者通過客服接口等API調用形式向用戶發送消息,則不受影響)。關於消息加解密的詳細說明,請見“消息加解密說明”。

 POST到開發者服務器上邊的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>

 接收消息數據包參數說明:

 

返回文本消息的XML格式: 

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

 

返回文本消息數據包參數說明:

 

②接收/返回文本消息代碼實現

 

開發者在自己服務器上邊接收微信服務器POST過來的XML數據包接收代碼如下:

 1 if(IsPostBack)
 2 { 
 3   //*********************************自動應答代碼塊*********************************
 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     //接收的消息為GBK格式
10     postString = Encoding.GetEncoding("GBK").GetString(postBytes);
11     string responseContent = help.ReturnMessage(postString );
12     //返回的消息為UTF-8格式
13     HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
14     HttpContext.Current.Response.Write(responseContent);
15   }
16   //********************************自動應答代碼塊end*******************************
17 }

注意:接收消息的時候要將消息格式轉化為“GBK”格式,否則後邊進行消息解析的時候沒辦法進行有效解析。

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

 1 /// <summary>
 2 /// 統一全局返回消息處理方法
 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);//菜單事件處理
18         break;
19       case "text":
20         responseContent = TextHandle(xmldoc);//文本消息處理
21         break;
22       default:
23         break;
24    }
25   }
26   return responseContent;
27 } 

 TextHandle(xmldoc)處理方法代碼如下:

 1 /// <summary>
 2 /// 接受文本消息並回復自定義消息
 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 == "指定回復消息的自定義文本")
15    {
16      responseContent = string.Format(XMLTemplate.Message_Text,
17        FromUserName.InnerText,
18        ToUserName.InnerText,
19        DateTime.Now.Ticks,
20        "自定義回復消息內容");
21    }
22  }
23  return responseContent;
24 }

到這裡實現功能的代碼演示已完畢,後邊其他的消息處理模式也是根據這種方式在做交互,比如:接收/回復文本消息、圖片消息、語音消息、視頻消息、小視頻消息、地理位置消息、鏈接消息等都可以參照以上代碼進行功能實現。

 


如果你對《C#微信開發系列》這個系列感興趣的話,可以關注我的其他文章(持續更新中...),系列隨筆地址如下:

《C#微信開發系列(Top)-微信開發完整學習路線》

《C#微信開發系列(4)-接收 / 返回文本消息》

《C#微信開發系列(3)-獲取接口調用憑據》

《C#微信開發系列(2)-自定義菜單管理》

《C#微信開發系列(1)-啟用開發者模式》


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved