這篇文章將為大家詳細(xì)講解有關(guān)C#微信開發(fā)如何實(shí)現(xiàn)接收 / 返回文本消息的方法,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
榆中網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,榆中網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為榆中上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的榆中做網(wǎng)站的公司定做!
接收 / 返回文本消息
①接收/返回文本消息原理說明
當(dāng)普通微信用戶向公眾賬號發(fā)消息時,微信服務(wù)器將POST消息的XML數(shù)據(jù)包到開發(fā)者填寫的URL上,著手開發(fā)之前先行閱讀微信公眾平臺接收普通消息微信開發(fā)文檔,對微信的這種消息處理機(jī)制有一定了解之后再著手開發(fā)(微信開發(fā)接收普通消息開發(fā)文檔)
注意點(diǎn):
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ù)說明:
返回文本消息的XML格式:
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[你好]]></Content> </xml>
返回文本消息數(shù)據(jù)包參數(shù)說明:
②接收/返回文本消息代碼實(shí)現(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ā)如何實(shí)現(xiàn)接收 / 返回文本消息的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
分享名稱:C#微信開發(fā)如何實(shí)現(xiàn)接收/返回文本消息的方法
文章網(wǎng)址:http://www.rwnh.cn/article46/jddheg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、自適應(yīng)網(wǎng)站、網(wǎng)頁設(shè)計公司、定制開發(fā)、云服務(wù)器、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)