微信公眾號接收消息和發送消息開發流程和注意事項


1.首先進行基本配置:

登錄 微信公眾平台 --》 開發 --》 基本配置,登錄網址 https://mp.weixin.qq.com,如下圖:

注意:在 http://www.xxx.com/wxmsg 頁面輸出 Response.Write(Request["echostr"]),提交之后方可通過驗證,通過驗證之后再修改該頁面為正式的業務邏輯。如果沒有輸出 Request["echostr"],將會出現“token驗證失敗”錯誤。

 

2. 接收消息,消息格式是xml,消息接口說明 http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html ,格式如:

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

注意:常規的Request是獲取不到消息的,我在網上查到的方式如下:
       public string PostInput(System.Web.HttpRequest request)
       {
           try
           {
               return PostInput(request, Encoding.UTF8);
           }
           catch (Exception ex)
           { throw ex; }
       }

       public string PostInput(System.Web.HttpRequest request, Encoding encoding)
       {
           StringBuilder builder = new StringBuilder();
           try
           {
               using (System.IO.Stream s = request.InputStream)
               {
                   int count = 0;
                   byte[] buffer = new byte[1024];
                   while ((count = s.Read(buffer, 0, 1024)) > 0)
                   {
                       builder.Append(encoding.GetString(buffer, 0, count));
                   }
                   s.Flush();
                   s.Close();
                   s.Dispose();
               }
               return builder.ToString();
           }
           catch (Exception ex)
           { throw ex; }
       }

 

3. 返回消息,格式也是xml,說明 http://mp.weixin.qq.com/wiki/14/89b871b5466b19b3efa4ada8e577d45e.html,直接 Response.Write() 輸出即可。

 注意:輸出之前先要Response.Clear(),否則可能由於緩存原因不輸出內容.

 



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM