当前位置: 代码迷 >> C# >> C#微信开发之旅-预备阶段
  详细解决方案

C#微信开发之旅-预备阶段

热度:35   发布时间:2016-05-05 04:26:18.0
C#微信开发之旅--准备阶段

最近才开始学微信开发的相关内容,记录下,慢慢的养成习惯!

1.申请公众号: 公众号分为 订阅号 和 服务号。他们之前的区别可以点击这里查看

因为我们是测试的,所以可以直接申请测试帐号,就把所有的功能都开通好了。 申请地址:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

申请了以后就有自己的

2.配置接收信息的URL地址(必须保证外网能访问到,微信需要验证),我这边只用handler来接收和处理信息。同时设置一个你自己的Token.(token值必须和你程序里面写的值是一致的)

/// <summary>    /// MarkWeixin 的摘要说明    /// </summary>    public class MarkWeixin : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            weixinHelper wx = new weixinHelper();            String postStr = String.Empty;            if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")            {                Stream s = HttpContext.Current.Request.InputStream;                Byte[] b = new Byte[s.Length];                s.Read(b, 0, (Int32)s.Length);                postStr = Encoding.UTF8.GetString(b);                if (!String.IsNullOrEmpty(postStr))                {                    //接收处理消息                    wx.Handle(postStr);                }            }            else            {                //验证token方法                wx.InterfaceTest();            }        }        public bool IsReusable        {            get            {                return false;            }        }    }

 

下面是验证方法: (配置里面的token值,就是这个方法里面的值)

/// <summary>        /// 验证        /// </summary>        public void InterfaceTest()        {            string token = "marktesttoken";            if (string.IsNullOrEmpty(token))            {                return;            }            string echoString = HttpContext.Current.Request.QueryString["echoStr"];            string signature = HttpContext.Current.Request.QueryString["signature"];            string timestamp = HttpContext.Current.Request.QueryString["timestamp"];            string nonce = HttpContext.Current.Request.QueryString["nonce"];            if (!string.IsNullOrEmpty(echoString))            {                HttpContext.Current.Response.Write(echoString);                HttpContext.Current.Response.End();            }        }

 

 结束语:简单的配置就这样完成了,后面就可以完善接收处理信息的方法!

  相关解决方案