微信開發基礎教程

微信開發學習筆記----

1.正確填寫服務器配置信息

其中Url爲咱們的要接收並處理微信服務器發送的消息的通常處理程序地址,如:http://sadi.qrenlei.cn/WXTest.ashx服務器

Token是一個開發者自定義的驗證字符串,可任意填寫。微信

點擊提交前,須要把我們的包含通常處理程序的網站發佈到服務器上。微信開發

2.通常處理程序的編寫函數

 

 

          if (Request.HttpMethod.ToLower() == "get")
            {
                Validate();
            } 

        public void Validate()
        {
                 //微信接口接入驗證代碼
                string signature = Request["signature"];
                string token = "Your token";
                string timestamp = Request["timestamp"];
                string nonce = Request["nonce"];
                string echostr = Request["echostr"];

                string[] temp = { token, timestamp, nonce };
                Array.Sort(temp);
                string str = string.Join("", temp);
                string sha1Str = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");

                if (sha1Str.ToLower() == signature.ToLower())
                {
                    Response.Write(echostr);
                }
        }

服務器端配置好後,點擊「提交」按鈕,就會提示成功接入的信息。post

3.接收消息

當普通微信用戶向公衆帳號發消息時,微信服務器將POST消息的XML數據包到開發者填寫的URL上。學習

            else if (Request.HttpMethod.ToLower() == "post")
            {
                //微信服務器發送信息是經過post請求,向發送者以流的形式發送xml
                Stream xmlStream = Request.InputStream;
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlStream);
                XmlElement root = doc.DocumentElement;
                string toUserName = root.SelectSingleNode("ToUserName").InnerText;
                string fromUserName = root.SelectSingleNode("FromUserName").InnerText;
                int createTime = int.Parse(root.SelectSingleNode("CreateTime").InnerText);
                string msgType = root.SelectSingleNode("MsgType").InnerText;
                string content = root.SelectSingleNode("Content").InnerText;
                long msgId = Int64.Parse(root.SelectSingleNode("MsgId").InnerText);
                
            }
     //由於服務器返回的是時間戳,即如今的時間與1970年1月1日8時0分0秒的秒數差,因此能夠用此函數對時間進行處理
public DateTime GetDateTime(int timeSpan) { return new DateTime(1970,1,1,8,0,0).AddSeconds(timeSpan); }

4.返回消息

//微信服務器接收信息是經過post請求,向接收者以流的形式發送xml
                /*
                 格式爲:
                    <xml>
                    <ToUserName><![CDATA[toUser]]>          </ToUserName>
                    <FromUserName><![CDATA[fromUser]]></FromUserName>
                    <CreateTime>12345678</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[你好]]></Content>
                    </xml>
                 */
                string reXml = string.Format(@"<xml>
                    <ToUserName><![CDATA[{0}]]></ToUserName>
                    <FromUserName><![CDATA[{1}]]></FromUserName>
                    <CreateTime>{2}</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[{3}]]></Content>
                    </xml>", fromUserName, toUserName, GetSecond(), "已接收到你的消息[服務器自動回覆]");
                Response.Write(reXml);     

       public int GetSecond()
          {
              return (int)(DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds;   } 
 

如今能夠測試一下你的公衆號,向公衆號發送一個文本消息,公衆號在5秒後會自動回覆你!測試

相關文章
相關標籤/搜索