好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#微信开发之接收 / 返回文本消息

接收 / 返回 文本消息

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

当普通微信用户向公众账号发消息时,微信服务器将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数据包接收代码如下:

?

if (ispostback)

{

    //*********************************自动应答代码块*********************************

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

  }

  //********************************自动应答代码块end*******************************

}

注意:接收消息的时候要将消息格式转化为[gbk]格式,否则后边进行消息解析的时候没办法进行有效解析。

returnmessage()处理方法代码如下:

?

/// <summary>

/// 统一全局返回消息处理方法

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

/// 接受文本消息并回复自定义消息

/// </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 == "指定回复消息的自定义文本" )

   {

     responsecontent = string .format(xmltemplate.message_text,

       fromusername.innertext,

       tousername.innertext,

       datetime.now.ticks,

        "自定义回复消息内容" );

   }

 }

  return responsecontent;

}

到这里实现功能的代码演示已完毕,后边其他的消息处理模式也是根据这种方式在做交互,比如:接收/回复文本消息、图片消息、语音消息、视频消息、小视频消息、地理位置消息、链接消息等都可以参照以上代码进行功能实现。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持服务器之家!

原文链接:http://HdhCmsTestcnblogs测试数据/likar/p/5247072.html

dy("nrwz");

查看更多关于C#微信开发之接收 / 返回文本消息的详细内容...

  阅读:55次