C#中怎么接收和返回文本消息-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)專注于中大型企業(yè)的成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)和網(wǎng)站改版、網(wǎng)站營(yíng)銷服務(wù),追求商業(yè)策劃與數(shù)據(jù)分析、創(chuàng)意藝術(shù)與技術(shù)開發(fā)的融合,累計(jì)客戶成百上千家,服務(wù)滿意度達(dá)97%。幫助廣大客戶順利對(duì)接上互聯(lián)網(wǎng)浪潮,準(zhǔn)確優(yōu)選出符合自己需要的互聯(lián)網(wǎng)運(yùn)用,我們將一直專注成都品牌網(wǎng)站建設(shè)和互聯(lián)網(wǎng)程序開發(fā),在前進(jìn)的路上,與客戶一起成長(zhǎng)!

4.0接收 / 返回文本消息

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

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

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

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#中怎么接收和返回文本消息

返回文本消息的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#中怎么接收和返回文本消息

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

開發(fā)者在自己服務(wù)器上邊接收微信服務(wù)器POST過來的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í)候沒辦法進(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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

文章標(biāo)題:C#中怎么接收和返回文本消息-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://www.muchs.cn/article38/djhjsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站建設(shè)網(wǎng)站改版、虛擬主機(jī)、動(dòng)態(tài)網(wǎng)站全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司