using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Xml;

namespace ITCastWeiXin_demo
{
    /// <summary>
    /// weixin 的摘要说明
    /// </summary>
    public class weixin : IHttpHandler
    {
        //private static string msg;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.HttpMethod.ToLower().Equals("get"))
            {
                //context.Response.Write(msg);
                //校验url
                ValidateUrl();
            }
            else
            {
                //接收并响应
                HandleMsg();
            }
        }

private void HandleMsg()
        {
            /*ToUserName 开发者微信号
FromUserName 发送方帐号(一个OpenID)
CreateTime 消息创建时间 (整型)
MsgType text
Content 文本消息内容
MsgId 消息id,64位整型*/
            HttpContext context = HttpContext.Current;
            //接收xml数据包
            Stream xmlStream = context.Request.InputStream;
            //构造xml对象
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlStream);
            XmlElement rootElement = doc.DocumentElement;//获取根节点
            //接收xml
            /*<xml>
 <ToUserName><![CDATA[toUser]]></ToUserName>
 <FromUserName><![CDATA[fromUser]]></FromUserName> 
 <CreateTime>1348831860</CreateTime>
 <MsgType><![CDATA[text]]></MsgType>
 <Content><![CDATA[this is a test]]></Content>
 <MsgId>1234567890123456</MsgId>
 </xml>
             */
            //解析xml数据
            string toUserName = rootElement.SelectSingleNode("ToUserName").InnerText;
            string fromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;
            string createTime = rootElement.SelectSingleNode("CreateTime").InnerText;
            string msgType = rootElement.SelectSingleNode("MsgType").InnerText;
            string content = rootElement.SelectSingleNode("Content").InnerText;
            string msgId = rootElement.SelectSingleNode("MsgId").InnerText;
            //msg = string.Format("{0}-{1}-{2}-{3}",toUserName,fromUserName,msgType,content);
            //响应
            /*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[郑州网站建设]]></Content>
</xml>
             */
            string xmlMsg = "<xml>" +
            "<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>" +
            "<FromUserName><![CDATA["+toUserName+"]]></FromUserName>" +
            "<CreateTime>12345678</CreateTime>" +
            "<MsgType><![CDATA[text]]></MsgType>" +
            "<Content><![CDATA[郑州网站建设]]></Content>" +
            "</xml>";
            context.Response.Write(xmlMsg);
            sendMsgToManage(fromUserName, content);//把用户发送的消息发送给管理员
        }
        private int GetCreateTime()
        {
            DateTime startDate = new DateTime(1970,1,1,8,0,0);
            return (int)(DateTime.Now - startDate).TotalSeconds;
        }
        private void ValidateUrl()
        {
            /*signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串*/
            //接收请求过来的参数
            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 = "weixin";
            string[] str1 = { token, timestamp, nonce };
            Array.Sort(str1);
            string str2 = string.Join("", str1);
            //加密
            string str3 = FormsAuthentication.HashPasswordForStoringInConfigFile(str2, "SHA1");
            if (str3.ToLower().Equals(signature))
            {
                context.Response.Write(echostr);
            }
        }
        private void sendMsgToManage(string toUserName,string content)
        {
            string managerweixinid = "wuchangjian001";
            string xmlMsg = "<xml>" +
            "<ToUserName><![CDATA[" + managerweixinid + "]]></ToUserName>" +
            "<FromUserName><![CDATA[" + toUserName + "]]></FromUserName>" +
            "<CreateTime>12345678</CreateTime>" +
            "<MsgType><![CDATA[text]]></MsgType>" +
            "<Content><![CDATA["+content+"]]></Content>" +
            "</xml>";
            HttpContext context = HttpContext.Current;
            context.Response.Write(xmlMsg);
        }

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

最新文章

  1. Oracle数据库Job的下次执行时间老是变动问题
  2. 通过代码获取res里生成R.java中的值
  3. 如何使用Nginx对抗DDoS攻击?
  4. jsonObject jsonarray
  5. ERP客户关系渠管理(二十)
  6. 如何用cufflinks 拼出一个理想的注释文件
  7. Dancing Stars on Me(判断正多边形)
  8. Mysql入门到精通数据表的操作
  9. 通过Func 委托理解委托和匿名方法及Lambda 表达式
  10. 【转】UWP 捕获全局异常
  11. Swift Runtime动态性分析
  12. IE6下最小19px像素
  13. python py2与py3的编码问题
  14. Flutter中打造多行列列表GridView组件的使用
  15. hibernate之三种时态之间的转换
  16. Log4net的不能产生Log文件的问题
  17. ubuntu apt 代理设置
  18. iOS-----使用CFNetwork实现TCP协议的通信
  19. js 命名空间
  20. Dubbo配置优化

热门文章

  1. jsp与El,jstl知识点总结归纳
  2. 深入Delphi -- Windows 消息机制
  3. CSS规则整理
  4. 一次JQuery性能优化实战
  5. Codeforces Gym 100803G Flipping Parentheses 线段树+二分
  6. Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones 水题
  7. 怎样基于android4.4.2的源代码和android-4.3.1_r1的驱动编译I9250的ROM
  8. Android progressBar 自定义圆形旋转图片
  9. jquery ui 改写cloes事件
  10. yii2-admin 插件使用简要教程