zoukankan      html  css  js  c++  java
  • 微信简单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;
                }
            }
        }
  • 相关阅读:
    request.getAttribute()和 request.getParameter()的区别
    jquery中$.get()提交和$.post()提交有区别吗?
    jQuery有几种选择器?
    jQuery 库中的 $() 是什么?
    JavaScript内置可用类型
    MySQL数据库中,常用的数据类型
    简单叙述一下MYSQL的优化
    什么是JDBC的最佳实践?
    Vue官网教程-条件渲染
    Vue官网教程-Class与Style绑定
  • 原文地址:https://www.cnblogs.com/crazyair/p/4331439.html
Copyright © 2011-2022 走看看