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;
                }
            }
        }
  • 相关阅读:
    系统权限控制模型
    [Golang] 剑走偏锋 -- IoComplete ports
    Golang 正则匹配 -- regexp
    golang -- 字符串就地取反
    Hyperledger Fabric chaincode 开发(疑难解答)
    could not launch process: decoding dwarf section info at offset 0x0: too short
    win10 Ubuntu16 双系统
    7-8 哈利·波特的考试
    7-7 六度空间
    7-6 列出连通集
  • 原文地址:https://www.cnblogs.com/crazyair/p/4331439.html
Copyright © 2011-2022 走看看