码迷,mamicode.com
首页 > 微信 > 详细

微信简单Demo

时间:2015-03-12 08:32:27      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

新建一个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

标签:

原文地址:http://www.cnblogs.com/crazyair/p/4331439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!