예제 #1
0
        public ActionResult Post(PostModel postModel)
        {
            if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, this.WeiXininfo.Token))
            {
                return Content("参数错误!");
            }

            postModel.Token = WeiXininfo.Token;
            postModel.EncodingAESKey = WeiXininfo.EncodingAESKey;//根据自己后台的设置保持一致
            postModel.AppId = WeiXininfo.appID;//根据自己后台的设置保持一致
            //每个人上下文消息储存的最大数量,防止内存占用过多,如果该参数小于等于0,则不限制
            var maxRecordCount = 10;
            var logPath = Server.MapPath(string.Format("~/App_Data/MP/{0}/", DateTime.Now.ToString("yyyy-MM-dd")));
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

            //自定义MessageHandler,对微信请求的详细判断操作都在这里面。
            var messageHandler = new CustomMessageHandler(Request.InputStream, postModel, maxRecordCount);
            try
            {

                //测试时可开启此记录,帮助跟踪数据,使用前请确保App_Data文件夹存在,且有读写权限。
                messageHandler.RequestDocument.Save(Path.Combine(logPath, string.Format("{0}_Request_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName)));
                if (messageHandler.UsingEcryptMessage)
                {
                    messageHandler.EcryptRequestDocument.Save(Path.Combine(logPath, string.Format("{0}_Request_Ecrypt_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName)));
                }
                messageHandler.OmitRepeatedMessage = true; //消息去重功能

                //执行微信处理过程
                messageHandler.Execute();

                //测试时可开启,帮助跟踪数据

                //if (messageHandler.ResponseDocument == null)
                //{
                //    throw new Exception(messageHandler.RequestDocument.ToString());
                //}

                if (messageHandler.ResponseDocument != null)
                {
                    messageHandler.ResponseDocument.Save(Path.Combine(logPath, string.Format("{0}_Response_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName)));
                }

                if (messageHandler.UsingEcryptMessage)
                {
                    //记录加密后的响应信息
                    messageHandler.FinalResponseDocument.Save(Path.Combine(logPath, string.Format("{0}_Response_Final_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName)));
                }
                return new FixWeiXinBugWeiXinResult(messageHandler);//为了解决官方微信5.0软件换行bug暂时添加的方法
            }
            catch (Exception ex)
            {
                using (TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".txt")))
                {
                    tw.WriteLine("ExecptionMessage:" + ex.Message);
                    tw.WriteLine(ex.Source);
                    tw.WriteLine(ex.StackTrace);
                    //tw.WriteLine("InnerExecptionMessage:" + ex.InnerException.Message);

                    if (messageHandler.ResponseDocument != null)
                    {
                        tw.WriteLine(messageHandler.ResponseDocument.ToString());
                    }

                    if (ex.InnerException != null)
                    {
                        tw.WriteLine("========= InnerException =========");
                        tw.WriteLine(ex.InnerException.Message);
                        tw.WriteLine(ex.InnerException.Source);
                        tw.WriteLine(ex.InnerException.StackTrace);
                    }

                    tw.Flush();
                    tw.Close();
                }
                return Content("");
            }
        }
예제 #2
0
 public ActionResult MiniPost(PostModel postModel)
 {
     if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, this.WeiXininfo.Token))
     {
         //return Content("参数错误!");//v0.7-
         return new WeiXinResult("参数错误!");//v0.8+
     }
     postModel.Token = WeiXininfo.Token;
     postModel.EncodingAESKey = WeiXininfo.EncodingAESKey;//根据自己后台的设置保持一致
     postModel.AppId = WeiXininfo.appID;//根据自己后台的设置保持一致
     var messageHandler = new CustomMessageHandler(Request.InputStream, postModel, 10);
     messageHandler.Execute();//执行微信处理过程
     return new FixWeiXinBugWeiXinResult(messageHandler);//v0.8+
 }