天天看点

微信成为开发者C#代码

第一句话都会这么去写:程序猿就是苦逼,除了开发还要会写博文!今天咱就和大家探讨下如何让自己成为开发者!那么怎么才能成为开发者呢?

首先给大家一个微信的截图,看到这个截图,是不是有想去尝试的冲动?

C#开发微信,一般都要写一个一般处理程序,就像截图中的.../CommonCS/WeiXin.ashx,这个一般处理程序会接收用户消息或开发者进行的事件推送。例如:用户关注时,微信服务器会将关注的数据包转发到咱们写的一般处理程序上。再例如:取消关注,用户发送文本消息,开发者群发消息等等吧!好多呢,在此不一一举例了。

现在进入正题,直接贴代码,然后再作讲解:

        #region 程序入口

        /// <summary>

        /// 程序入口

        /// </summary>

        /// <param name="context"></param>

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            try

            {

                if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET")

                {

                    Auth(); //微信接入的测试  成为开发者第一步

                }

                if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")

            }

            catch(Exception ex)

                LogHelper.WriteLog("系统故障。", ex);

        }

        public bool IsReusable

            get

                return false;

        #endregion

LogHelper.WriteLog("系统故障。", ex);在此,我使用的是Log4Net,用于记录系统出现的错误。当然你也可以自己写一个日志类,不过没有Log4Net强大罢了。在此,我提供一些日志方法,仅供参考。如下:

#region 日志类 记录异常和消息  注:在本系统中,请勿使用。请使用Log4Net

        #region 字段

        public static object _lock = new object();

        #region 写文件

        /// 写文件

        public static void WriteFile(string log, string path)

            Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)

                lock (_lock)

                    if (!File.Exists(path))

                    {

                        using (FileStream fs = new FileStream(path, FileMode.Create)) { }

                    }

                    using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))

                        using (StreamWriter sw = new StreamWriter(fs))

                        {

                            #region 日志内容

                            string value = string.Format(@"{0} {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), obj.ToString());

                            #endregion

                            sw.WriteLine(value);

                            sw.Flush();

                        }

            }));

            thread.Start(log);

        #region 写日志

        /// 写日志

        public static void WriteLog(HttpContext context, string log)

            string logPath = context.Request.MapPath(@"\CommonCS\WX_Log.txt");

            WriteFile(log, logPath);

        #region 写错误日志

        /// 写错误日志

        public static void WriteErrorLog(HttpContext context, string log)

            string logPath = context.Request.MapPath(@"\CommonCS\WX_ErrorLog.txt");

#region 成为开发者

        /// 验证微信签名

        public bool CheckSignature(string token, string signature, string timestamp, string 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

        /// 成为开发者的第一步,验证并相应服务器的数据

        private void Auth()

            string token = GetXMLstr("URLToken");//自己填写的Token  和开启开发者模式时填写的一样。

            if (string.IsNullOrEmpty(token))

                //LogTextHelper.Error(string.Format("WeixinToken 配置项没有配置!"));

            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 (CheckSignature(token, signature, timestamp, nonce))

                if (!string.IsNullOrEmpty(echoString))

                    HttpContext.Current.Response.Write(echoString);

                    // HttpContext.Current.Response.End();

将上述代码复制粘贴到你的一般处理程序中。

上述红色部分,和你启用开发者模式时填写的一样。不过,我将这些信息放在了XML文件中,你也可以直接赋值给它,例如:

string token="shengshiguanqian";//截图如下:你懂得。

这些代码网上很多,自己写好这个处理程序后,发布到服务器,然后按照要求进行微信开发的服务器配置,路径要写对,Token要和开发中的Token一样。也就是本博客中的红色字体。你懂得。

后续,我还会陆陆续续的写一些关于微信的博客。在此,感谢做C#的大神公开的代码,今天我学会了,也公开啦!无私奉献是一个程序员最美的美德。嘻嘻。

哈哈,嘻嘻,我的蔻蔻:1429677330.有需要源码的可以加一下。谢谢。