微信简单Demo

新建一个WxHandler.ashx

 

   public class WxHandler : IHttpHandler
    {
        public static string Msg;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.HttpMethod.ToLower().Equals("get"))
            {
                context.Response.Write(Msg);
                //校验
                VaildateUrl();
            }
            else
            {
                //接受并相应
                HandleMsg();
            }
        }

        private void HandleMsg()
        {
            HttpContext context = HttpContext.Current;
            Stream xmlStream = context.Request.InputStream;
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlStream);
            XmlElement rootElement = doc.DocumentElement;
            string toUserName = rootElement.SelectSingleNode("ToUserName").InnerText;
            string fromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;
            string msgType = rootElement.SelectSingleNode("MsgType").InnerText;
            string content = rootElement.SelectSingleNode("Content").InnerText;
            //Msg = string.Format("{0}--{1}--{2}---{3}",toUserName,fromUserName,msgType,content);
            string xmlMsg = "<xml>" +
                            "<ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>" +
                            "<FromUserName><![CDATA[" + toUserName + "]]></FromUserName>" +
                            "<CreateTime>" + GetCreateTime() + "</CreateTime>" +
                            "<MsgType><![CDATA[text]]></MsgType>" +
                            "<Content><![CDATA[亲爱的你给我说的是:" + content + ",你说这是什么意思呢?]]></Content></xml>";
            Msg = xmlMsg;
            context.Response.Write(xmlMsg);
        }
        private int GetCreateTime()
        {
            DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
            return (int)(DateTime.Now - dateStart).TotalSeconds;
        }
        private void VaildateUrl()
        {
            HttpContext context = HttpContext.Current;
            string signature = context.Request["signature"];
            string timestamp = context.Request["timestamp"];
            string nonce = context.Request["nonce"];
            string echostr = context.Request["echostr"];
            string token = "huang";
            string[] temp1 = { token, timestamp, nonce };
            Array.Sort(temp1);
            string temp2 = string.Join("", temp1);
            string temp3 = FormsAuthentication.HashPasswordForStoringInConfigFile(temp2, "SHA1");
            if (temp3.ToLower().Equals(signature))
            {
                context.Response.Write(echostr);
            }
            else
            {
                context.Response.Write("浏览器打开方式!!");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

微信简单Demo

上一篇:买手经济的消息同步框架[参考微信的消息同步]


下一篇:仿微信小视频录制控件