public CustomMessageHandler(Stream inputStream, Senparc.Weixin.MP.Entities.Request.PostModel postModel = null, int maxRecordCount = 0) : base(inputStream, postModel, maxRecordCount) { //这里设置仅用于测试,实际开发可以在外部更全局的地方设置, //比如MessageHandler<MessageContext>.GlobalWeixinContext.ExpireMinutes = 3。 WeixinContext.ExpireMinutes = 10; WeixinContext.MaxRecordCount = 1000; //最多存储多少条记录 }
public IActionResult PostAction(Senparc.Weixin.MP.Entities.Request.PostModel postModel) { if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Senparc.Weixin.Config.SenparcWeixinSetting.Token)) { return(Content("参数错误!")); } postModel.Token = Senparc.Weixin.Config.SenparcWeixinSetting.Token; //根据自己后台的设置保持一致 postModel.EncodingAESKey = Senparc.Weixin.Config.SenparcWeixinSetting.EncodingAESKey; //根据自己后台的设置保持一致 postModel.AppId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId; //根据自己后台的设置保持一致 //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var messageHandler = new WxMPMessageHandler(Request.Body, postModel); //接收消息 messageHandler.OmitRepeatedMessage = true; //启用消息去重功能 messageHandler.Execute(); //执行微信处理过程 return(new FixWeixinBugWeixinResult(messageHandler)); //返回结果 }
public ActionResult Callback(Senparc.Weixin.MP.Entities.Request.PostModel postModel) { //此处的URL格式类型为:http://sdk.weixin.senparc.com/Open/Callback/$APPID$, 在RouteConfig中进行了配置,你也可以用自己的格式,只要和开放平台设置的一致。 //处理微信普通消息,可以直接使用公众号的MessageHandler。此处的URL也可以直接填写公众号普通的URL,如本Demo中的/Weixin访问地址。 var logPath = ServerUtility.ContentRootMapPath(string.Format("~/App_Data/Open/{0}/", SystemTime.Now.ToString("yyyy-MM-dd"))); if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } postModel.Token = component_Token; postModel.EncodingAESKey = component_EncodingAESKey; //根据自己后台的设置保持一致 postModel.AppId = component_AppId; //根据自己后台的设置保持一致 var maxRecordCount = 10; MessageHandler <CustomMessageContext> messageHandler = null; try { var checkPublish = false; //是否在“全网发布”阶段 if (checkPublish) { messageHandler = new OpenCheckMessageHandler(Request.GetRequestMemoryStream(), postModel, 10); } else { messageHandler = new CustomMessageHandler(Request.GetRequestMemoryStream(), postModel, maxRecordCount); } messageHandler.SaveRequestMessageLog(); //记录 Request 日志(可选) messageHandler.Execute(); //执行微信处理过程(关键) messageHandler.SaveResponseMessageLog(); //记录 Response 日志(可选) return(new FixWeixinBugWeixinResult(messageHandler)); } catch (Exception ex) { using ( TextWriter tw = new StreamWriter(ServerUtility.ContentRootMapPath("~/App_Data/Open/Error_" + SystemTime.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("")); } } }
public ActionResult Callback(Senparc.Weixin.MP.Entities.Request.PostModel postModel) { //此处的URL格式类型为:http://wx.wftx666.com/Open/Callback/$APPID$, 在RouteConfig中进行了配置,你也可以用自己的格式,只要和开放平台设置的一致。 //处理微信普通消息,可以直接使用公众号的MessageHandler。此处的URL也可以直接填写公众号普通的URL,如本Demo中的/Weixin访问地址。 var logPath = Server.MapPath(string.Format("~/App_Data/Open/{0}/", DateTime.Now.ToString("yyyy-MM-dd"))); if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } WeixinTrace.SendCustomLog("记录APPID", "CallbackAppId:" + postModel.AppId); postModel.Token = component_Token; postModel.EncodingAESKey = component_EncodingAESKey; postModel.AppId = component_AppId; var maxRecordCount = 10; MessageHandler <CustomMessageContext> messageHandler = null; try { //是否在“全网发布”阶段 var checkPublish = false; if (checkPublish) { //全网发布测试处理 messageHandler = new OpenCheckMessageHandler(Request.InputStream, postModel, maxRecordCount); } else { //处理公众号消息 messageHandler = new CustomMessageHandler(Request.InputStream, postModel, maxRecordCount); } messageHandler.RequestDocument.Save(Path.Combine(logPath, string.Format("{0}_CallbackRequest_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName))); messageHandler.Execute(); //执行 if (messageHandler.ResponseDocument != null) { var ticks = DateTime.Now.Ticks; messageHandler.ResponseDocument.Save(Path.Combine(logPath, string.Format("{0}_CallbackResponse_{1}.txt", ticks, messageHandler.RequestMessage.FromUserName))); } return(new FixWeixinBugWeixinResult(messageHandler)); } catch (Exception ex) { using ( TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Open/CallbackError_" + DateTime.Now.Ticks + ".txt"))) { tw.WriteLine("ExecptionMessage:" + ex.Message); tw.WriteLine(ex.Source); tw.WriteLine(ex.StackTrace); 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("")); } } }
protected void Page_Load(object sender, EventArgs e) { int uid = DTRequest.GetQueryInt("uid", 0); if (uid == 0) { WriteContent("参数非法"); return; } Model.weixin_account model = new BLL.weixin_account().GetModel(uid); //获取公众账户信息 if (model == null || string.IsNullOrEmpty(model.token)) { WriteContent("不存在的公众账户!"); return; } //获取微信传送参数 string signature = Request["signature"]; string timestamp = Request["timestamp"]; string nonce = Request["nonce"]; string echostr = Request["echostr"]; if (Request.HttpMethod == "GET") { //GET-仅在微信后台填写URL验证时触发 if (Senparc.Weixin.MP.CheckSignature.Check(signature, timestamp, nonce, model.token)) { WriteContent(echostr); //返回随机字符串则表示验证通过 } else { WriteContent("failed:" + signature + ",token:" + model.token + " " + Senparc.Weixin.MP.CheckSignature.GetSignature(timestamp, nonce, model.token) + "。" + "如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。"); } Response.End(); } else { if (!Senparc.Weixin.MP.CheckSignature.Check(signature, timestamp, nonce, model.token)) { WriteContent("参数错误!"); return; } //post method - 当有用户想公众账号发送消息时触发 var postModel = new Senparc.Weixin.MP.Entities.Request.PostModel() { Signature = Request.QueryString["signature"], Msg_Signature = Request.QueryString["msg_signature"], Timestamp = Request.QueryString["timestamp"], Nonce = Request.QueryString["nonce"], //以下保密信息不会(不应该)在网络上传播,请注意 Token = model.token, //EncodingAESKey = model.aeskey, //根据自己后台的设置保持一致 AppId = model.appid //根据自己后台的设置保持一致 }; //v4.2.2之后的版本,可以设置每个人上下文消息储存的最大数量,防止内存占用过多,如果该参数小于等于0,则不限制 var maxRecordCount = 10; //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var messageHandler = new CustomMessageHandler(Request.InputStream, postModel, maxRecordCount); try { //执行微信处理过程 messageHandler.Execute(); WriteContent(messageHandler.ResponseDocument.ToString()); return; } catch { /*using (TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".txt"))) * { * tw.WriteLine(ex.Message); * tw.WriteLine(ex.InnerException.Message); * if (messageHandler.ResponseDocument != null) * { * tw.WriteLine(messageHandler.ResponseDocument.ToString()); * } * tw.Flush(); * tw.Close(); * }*/ } finally { Response.End(); } } }
public ActionResult Callback(Senparc.Weixin.MP.Entities.Request.PostModel postModel) { //此处的URL格式类型为:http://sdk.weixin.senparc.com/Open/Callback/$APPID$, 在RouteConfig中进行了配置,你也可以用自己的格式,只要和开放平台设置的一致。 //处理微信普通消息,可以直接使用公众号的MessageHandler。此处的URL也可以直接填写公众号普通的URL,如本Demo中的/Weixin访问地址。 var logPath = hostingEnvironment.MapPath(string.Format("~/App_Data/Open/{0}/", DateTime.Now.ToString("yyyy-MM-dd"))); if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } postModel.Token = options.Token; postModel.EncodingAESKey = options.EncodingAESKey; //根据自己后台的设置保持一致 postModel.AppId = options.AppId; //根据自己后台的设置保持一致 var maxRecordCount = 10; MessageHandler <CustomMessageContext> messageHandler = null; try { string body = new StreamReader(Request.Body).ReadToEnd(); byte[] requestData = Encoding.UTF8.GetBytes(body); Stream inputStream = new MemoryStream(requestData); var checkPublish = false; //是否在“全网发布”阶段 if (checkPublish) { messageHandler = new OpenCheckMessageHandler(inputStream, postModel, options.AppId, options.AppSecret, hostingEnvironment, 10); } else { messageHandler = new CustomMessageHandler(inputStream, postModel, options.AppId, options.AppSecret, hostingEnvironment, maxRecordCount); } messageHandler.RequestDocument.Save(Path.Combine(logPath, string.Format("{0}_Request_{1}.txt", DateTime.Now.Ticks, messageHandler.RequestMessage.FromUserName))); messageHandler.Execute(); //执行 if (messageHandler.ResponseDocument != null) { var ticks = DateTime.Now.Ticks; messageHandler.ResponseDocument.Save(Path.Combine(logPath, string.Format("{0}_Response_{1}.txt", ticks, messageHandler.RequestMessage.FromUserName))); //记录加密后的日志 //if (messageHandler.UsingEcryptMessage) //{ // messageHandler.FinalResponseDocument.Save(Path.Combine(logPath, // string.Format("{0}_Response_Final_{1}.txt", ticks, // messageHandler.RequestMessage.FromUserName))); //} } return(new FixWeixinBugWeixinResult(messageHandler)); } catch (Exception ex) { using ( TextWriter tw = new StreamWriter(hostingEnvironment.MapPath("~/App_Data/Open/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("")); } } }