/// <summary> /// 从请求中获取上下文信息。 /// 如果有扩展,请同时扩展此方法。 /// </summary> /// <returns></returns> private static InvokerContext ParseFromContext() { return(new InvokerContext { Message = ReceiveMessage.ParseFromContext() }); }
public void ParseMessage_Success() { var text = @"<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1351776360</CreateTime> <MsgType><![CDATA[link]]></MsgType> <Title><![CDATA[公众平台官网链接]]></Title> <Description><![CDATA[公众平台官网链接]]></Description> <Url><![CDATA[url]]></Url> <MsgId>1234567890123456</MsgId> </xml>"; //ResponseMessage rep = new RepTextMessage(); //rep.Content = "sd"; //msg2.GetMusicResponse(); //msg2.GetNewsResponse(); //ReceiveMessage.ResisterHandler(handler); var msg2 = ReceiveMessage.ParseFromContext(); ResponseMessage rep = msg2.Process();//switch来执行不同消息类型的方法 //var rep = msg2.GetTextResponse(); //rep.Data = (TextMsgData)"这是响应的文本内容"; rep.Response(); //Assert.AreEqual(MessageType.Link, msg.MsgType); }
/// <summary> /// 放在接收数据的页面即可 实现WxMsgHandler类的消息 /// </summary> public static void StartAcceptRequest() { if (!ReceiveMessage.IsRgisterMessageHandler) { ReceiveMessage.ResisterHandler(new WxMsgHandler()); } if (ReceiveMessage.IsRgisterMessageHandler) { var sign = EntrySign.ParseFromContext(); //验签名 if (sign.Check()) { //如果是GET请求就返回echostr数据 if (EntrySign.IsEntryCheck()) { sign.Response(); } else { //其他请求交给注册的实现接口IMessageHandler的类完成 var msg = ReceiveMessage.ParseFromContext(); var rep = msg.Process(); rep.Response(); } } } }
//public static string RmRequest = null; //public ResponseTextMessage RmResponse = null; //[HttpGet] //public ActionResult Index() //{ // //WeiXinBase weixin = WeiXinBase.getInstance(); // //return Content(weixin.Access_Token); // var sign = EntrySign.ParseFromContext(); // if (/*sign.Check()*/true) // { // if (EntrySign.IsEntryCheck()) // { // sign.Response(); // } // else // { // var msg = ReceiveMessage.ParseFromContext(); // var rep = msg.Process(); // rep.Response(); // } // } // //return Content("request:" + rq + "\r\n" + "response:" + rp + "\r\n" + "xmlrequest:" + xmlResult); // return View(); //} public ActionResult Index() { //注册事件处理程序 ReceiveMessage.ResisterHandler(new WxMsgHandler()); //var request = System.Web.HttpContext.Current.Request; //var sr = new StreamReader(request.InputStream); //var msg = Parse(); //string text = sr.ReadToEnd(); //var e = XElement.Parse(text); //var t = e.Element("MsgType").Value; //var sign = EntrySign.ParseFromContext(); string respose = "回复出错!"; //if (/*sign.Check()*/true) //{ // if (/*EntrySign.IsEntryCheck()*/false) // { // sign.Response(); // } // else // { //LogHelper.Log(text); var msg = ReceiveMessage.ParseFromContext(); //LogHelper.Log(msg.ToString()); var rep = msg.Process(); //LogHelper.Log(rep.ToXmlText()); respose = rep.Response(); // } //} return(Content(respose)); }