asp.net微信公众平台

进入微信公众平台网站,这里用测试账号进行演示

点击开发者中心>公众平台测试账号>接口配置信息


在这里输入url和token

测试通过就可以使用填写的url作为服务端来操作了。

下面主要介绍服务端代码

首页代码:

 /// <summary>
    /// IndexHander 的摘要说明
    /// </summary>
    public class IndexHander : IHttpHandler
    {
        private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        //http://blueummser-001-site1.site4future.com/IndexHander.ashx
        public void ProcessRequest(HttpContext context)
        {
            Log.Debug("\ncomein");
            //由微信服务接收请求,具体处理请求
            WeiXinService wxService = new WeiXinService(context.Request);
            string responseMsg = wxService.Response();
            context.Response.Clear();
            context.Response.Charset = "UTF-8";
            context.Response.Write(responseMsg);
            context.Response.End();
            Log.Debug("responseMsg:" + responseMsg);
            Log.Debug("comeout\n");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
Services代码:注:除去token验证之外,所有的请求方式都为post方式,,微信服务器发来的是一串xml字符串,这里将其转换为xml文档

 public class WeiXinService
    {
        private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static readonly string TOKEN = "bluesummer";
        private HttpRequest Request;

        public WeiXinService(HttpRequest httpRequest)
        {
            Request = httpRequest;
        }

        public string Response()
        {
            string method = Request.HttpMethod.ToUpper();
            Log.Debug("method:" + method);
            //验证签名
            if (method == "GET")
            {
                bool checkResult = CheckSignature();
                Log.Debug("checkResult:" + checkResult);
                if (checkResult)
                    return Request.QueryString["echostr"];
                else
                    return "error";
            }

            //处理消息
            if (method == "POST")
                return ResponseMsg();
            else
                return "无法处理";

        }
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <returns></returns>
        private string ResponseMsg()
        {
            string requestXml = Common.ReadRequest(this.Request);
            Log.Debug("requestXml:" + requestXml);
            IHandler handler = HanderFactory.CreateHander(Common.ConvertXmlStr(requestXml));
            if (handler != null)
            {
                return handler.HandleRequest();
            }

            return string.Empty;
        }
        /// <summary>
        /// 检查签名
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private bool CheckSignature()
        {
            string signature = this.Request.QueryString["signature"];
            string timestamp = this.Request.QueryString["timestamp"];
            string nonce = this.Request.QueryString["nonce"];
            Log.Debug("signature:" + signature);
            Log.Debug("timestamp:" + timestamp);
            Log.Debug("nonce:" + nonce);
            string[] ArrTmp = { TOKEN, timestamp, nonce };
            Array.Sort(ArrTmp);     //字典排序
            string tmpStr = string.Join("", ArrTmp);
            tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
            tmpStr = tmpStr.ToLower();
            if (tmpStr == signature)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="intput">输入字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string SHA1Encrypt(string intput)
        {
            byte[] StrRes = Encoding.Default.GetBytes(intput);
            HashAlgorithm mySHA = new SHA1CryptoServiceProvider();
            StrRes = mySHA.ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte Byte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", Byte);
            }
            return EnText.ToString();
        }

    }
HandlerFactory:主要负责事件和普通文本两种请求的处理

  public class HanderFactory
    {
        private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static IHandler CreateHander(XmlDocument xmldoc)
        {
            XmlNode MsgType = xmldoc.SelectSingleNode("/xml/MsgType");
            Log.Debug("MsgType:" + MsgType.InnerText);
            if (MsgType != null)
            {
                switch (MsgType.InnerText.ToLower())
                {
                    case "event":
                        return new EventHandler(xmldoc);//事件处理
                    case "text":
                       return new TextHandler(xmldoc);;//接受文本消息处理
                    default:
                        break;
                }
            }
            return null;
        }
    }
附上texthandler代码:

 public class TextHandler : IHandler
    {
        private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 请求的xml
        /// </summary>
        private XmlDocument RequestXml { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="requestXml"></param>
        public TextHandler(XmlDocument xmlDoc)
        {
            this.RequestXml = xmlDoc;
        }
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <returns></returns>
        public string HandleRequest()
        {
            Log.Debug("TextHander:HandleRequest");
            string response = string.Empty;
            try
            {
                TextMessage tm = TextMessage.LoadFromXml(RequestXml);
                IReplay replaydemo = new IReplay()
                {
                    ToUserName = tm.FromUserName,
                    FromUserName = tm.ToUserName,
                    CreateTime = Common.GetNowTime()
                };

                IReplay replay = new TextReplay(replaydemo, "文字格式错误"); ;
                Log.Debug("TextHander:new TextMessage()ok");
                switch (tm.Content)
                {
                    case "你好":
                        replay = new TextReplay(replaydemo, "你好");
                        break;
                    case "图文列表":
                        List<NewsItem> list = new List<NewsItem>();
                        NewsItem item = new NewsItem("title", "descrip", "http://b.hiphotos.baidu.com/image/pic/item/9f510fb30f2442a7f0a17475d243ad4bd01302b6.jpg", "http://www.soso.com/");
                        NewsItem item2 = new NewsItem("title2", "descrip2", "http://d.hiphotos.baidu.com/image/pic/item/f7246b600c33874438f06f7d530fd9f9d72aa0fe.jpg", "http://www.soso.com/");
                        list.Add(item); list.Add(item2);
                        replay = new NewsMainReplay(replaydemo, "2", list);
                        break;
                }
                Log.Debug("TextHander:new TextReplay()ok");
                response = replay.GetReplayXml();
                Log.Debug("response:" + response);
            }
            catch (Exception e)
            {
                Log.Debug("Exception:" + e.Message);
            }
            return response;
        }
    }
回复的文字格式同样要以xml格式回复

 public class TextReplay : IReplay
    {
        private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
       
        public string Content { get; set; }

        public TextReplay(IReplay replay, string Content)
        {
            this.ToUserName = replay.ToUserName;
            this.FromUserName = replay.FromUserName;
            this.CreateTime = replay.CreateTime;
            this.Content = Content;
        }
        public override string GetReplayXml()
        {
            Log.Debug("come TextReplay GetReplayXml()");
            string xmlStr = @"<xml>
                    <ToUserName><![CDATA[{0}]]></ToUserName>
                    <FromUserName><![CDATA[{1}]]></FromUserName>
                    <CreateTime>{2}</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[{3}]]></Content>
                    </xml>";
            return string.Format(xmlStr, ToUserName, FromUserName, CreateTime, Content);
        }
    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
概述 本平台主要功能是针对微信商家公众号提供与众不同的、有针对性的营销推广服务。通过沐雪微信平台,用户可以轻松管理自己的微信各类信息,对微信公众账号进行维护、开展智能机器人、在线发优惠劵、抽奖、刮奖、派发会员卡、打造微官网、开启微团购等多种活动,对微信营销实现有效监控,极大扩展潜在客户群和实现企业的运营目标。沐雪微信平台很好的弥补了微信公众平台本身功能不足、针对性不强、交互不便利的问题,为商家公众账号提供更为贴心的、且是核心需求的功能和服务。在线优惠劵、转盘抽奖、微信会员卡等推广服务更是让微信成为商家推广的利器。智能客服的可调教功能让用户真正从微信繁琐的日常客服工作中解脱出来,真正成为商家便利的新营销渠道。 二、源码特点 1、微信公众平台开发,对于公众平台所有功能进行完整开发。 2、多用户:可同时进行多公众号的管理和配置。 3、直接性:购买者可直接购买细微修改即是成品的平台商品。 4、开发语言:asp.net,C# ,webform,数据库sqlserver 2005 三、功能介绍 1、菜单回复:关注时回复、默认回复、文本回复、图文回复、语音回复、请求回复、LBS回复。 2、自定义菜单:公众号自定义菜单设置 3、用户列表管理:获取关注公众号的账户信息进行管理 4、微官网:各种微官网模板可自行配置,头部幻灯片,底部,分类模块配置,内容配置 5、微商城:商城模板配置、产品分类管理、商品信息管理、商品录入、客户资源管理、订单管 理、物流配送方式设置 6、支付方式:微信支付,货到付款。 7、微汽车[大模块]、微酒店、点餐系统 8、微会员卡:会员卡商家、会员卡商城、会员卡头部广告设计 9、用户管理:微信用户管理、系统角色管理、系统操作日志 10、后台菜单:后台导航菜单管理 11、系统设置:网站基本信息设置、功能权限设置、短信平台设置、邮件发送设置、文件上传设 置 12、代理商管理:代理商发展下线。 四、环境要求 开发环境: 操作系统:windows7,8 软件:vs2012+sqlserver2005 发布环境: 操作系统:windows server 2003,2008 软件:.netframework4.0,iis6+,sqlserver 2005 后续会更新vs2017+sql2012 ,新功能的开发
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值