C#微信开发---用户关注微信号后公众号发送欢迎关注

    用户关注微信或者取消关注微信的时候,微信服务器会发送一个 post请求到微信配置中的URL地址中。可以提取其中 的数据判断是关注还是取消

推送XML数据包示例:

<xml><ToUserName>< ![CDATA[toUser] ]></ToUserName><FromUserName>< ![CDATA[FromUser] ]></FromUserName><CreateTime>123456789</CreateTime><MsgType>< ![CDATA[event] ]></MsgType><Event>< ![CDATA[subscribe] ]></Event></xml>

------------------------------------------------------------------------------------------------------------------------------

 //1:总方法

 private void subscribe()
        {
            //1:得到发送过来的数据
            string inMessage = GetSendInMessage(Request);
            //2:获取xml中的数据
            WX_In_MessageParent wX_In = new WX_In_MessageParent(inMessage);
            //3:判断是否是订阅或取消订阅事件
            if (WX_in_MessageManager.getTagNameContent(inMessage, WX_In_Filed.Event.ToString()) != "")
            {
                //2:执行发送信息的方法
                Event(inMessage, wX_In);
            }
        }
//2: 引用方法
 private string GetSendInMessage(HttpRequestBase requestBase)
        {
            using (Stream stream = requestBase.InputStream)
            {
                //创建一个字节数字,用来保存读取的字符
                Byte[] postBytes = new Byte[stream.Length];
                //把传递过来的内容保存(读取)到postBytes字符数组中
                stream.Read(postBytes, 0, (Int32)stream.Length);
                //得到传递过来的内容。
                string postString = Encoding.UTF8.GetString(postBytes);


                return postString;
            }
        }
//3:类  WX_In_MessageParent
 public class WX_In_MessageParent
    {
        private string toUserName;
        private string fromUserName;
        private string createTime;
        private string msgType;
        private string msgId;

        public string ToUserName
        {
            get
            {
                return toUserName;
            }

            set
            {
                toUserName = value;
            }
        }

        public string FromUserName
        {
            get
            {
                return fromUserName;
            }

            set
            {
                fromUserName = value;
            }
        }

        public string CreateTime
        {
            get
            {
                return createTime;
            }

            set
            {
                createTime = value;
            }
        }

        public string MsgType
        {
            get
            {
                return msgType;
            }

            set
            {
                msgType = value;
            }
        }

        public string MsgId
        {
            get
            {
                return msgId;
            }

            set
            {
                msgId = value;
            }
        }

        /*通过构造函数来获取服务器发送过来的信息*/
        public WX_In_MessageParent(string requestStr)
        {
            ToUserName = WX_in_MessageManager.getTagNameContent(requestStr, WX_In_Filed.ToUserName.ToString());
            FromUserName = WX_in_MessageManager.getTagNameContent(requestStr, WX_In_Filed.FromUserName.ToString());
            CreateTime = WX_in_MessageManager.getTagNameContent(requestStr, WX_In_Filed.CreateTime.ToString());
            MsgType = WX_in_MessageManager.getTagNameContent(requestStr, WX_In_Filed.MsgType.ToString());
            MsgId = WX_in_MessageManager.getTagNameContent(requestStr, WX_In_Filed.MsgId.ToString());

        }
    }
//4:事件
 private void  Event(string inMessage, WX_In_MessageParent WX_In)
        {
            string eventName = WX_in_MessageManager.getTagNameContent(inMessage, WX_In_Filed.Event.ToString());
            switch (eventName)
            {
                case "subscribe"://订阅
                   Response.Write(WX_MessageCode.WX_Subscribe(WX_In));
                    break;
                case "unsubscribe"://取消订阅
                    WX_MessageCode.WX_UnSubscribe(WX_In);
                    break;
                default:
                    break;
            }

        }

//5:推送方法

 public static string WX_Subscribe(WX_In_MessageParent wxobj)
        {
            WX_Out_MessageText wx_out = new WX_Out_MessageText(wxobj, "欢迎关注****");
            return wx_out.GetReturnStr();
        }
   public override string GetReturnStr()
        {
          
            //Content就是刚才传进来的数据,这个地方是调用的字段,类WX_In_MessageParent中的字段
            StringBuilder strbuff = new StringBuilder();
            strbuff.Append("<xml>");
            strbuff.Append("<ToUserName><![CDATA[" + ToUserName + "]]></ToUserName>");
            strbuff.Append("<FromUserName><![CDATA[" + FromUserName + "]]></FromUserName>");
            strbuff.Append("<CreateTime>" + CreateTime + "</CreateTime>");
            strbuff.Append("<MsgType><![CDATA[" + MsgType + "]]></MsgType>");
            strbuff.Append("<Content><![CDATA[" + Content + "]]></Content>");
            strbuff.Append("</xml>");
            return strbuff.ToString();
        }
写得比较散,不好全部拿出来,只取了核心的方法。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值