예제 #1
0
 //<?xml version="1.0" encoding="utf-8"?>
 //<xml>
 //  <ToUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></ToUserName>
 //  <FromUserName><![CDATA[gh_a96a4a619366]]></FromUserName>
 //  <CreateTime>63497820384</CreateTime>
 //  <MsgType>text</MsgType>
 //  <Content><![CDATA[您刚才发送了文字信息:中文
 //您还可以发送【位置】【图片】【语音】信息,查看不同格式的回复。
 //SDK官方地址:http://weixin.senparc.com]]></Content>
 //  <FuncFlag>0</FuncFlag>
 //</xml>
 /// <summary>
 /// 获取XDocument转换后的IResponseMessageBase实例(通常在反向读取日志的时候用到)。
 /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
 /// </summary>
 /// <returns></returns>
 public static IResponseMessageBase GetResponseEntity(XDocument doc)
 {
     ResponseMessageBase responseMessage = null;
     ResponseMsgType msgType;
     try
     {
         msgType = MsgTypeHelper.GetResponseMsgType(doc);
         switch (msgType)
         {
             case ResponseMsgType.Text:
                 responseMessage = new ResponseMessageText();
                 break;
             case ResponseMsgType.Image:
                 responseMessage = new ResponseMessageImage();
                 break;
             case ResponseMsgType.Voice:
                 responseMessage = new ResponseMessageVoice();
                 break;
             case ResponseMsgType.Video:
                 responseMessage = new ResponseMessageVideo();
                 break;
             case ResponseMsgType.Music:
                 responseMessage = new ResponseMessageMusic();
                 break;
             case ResponseMsgType.News:
                 responseMessage = new ResponseMessageNews();
                 break;
             case ResponseMsgType.Transfer_Customer_Service:
                 responseMessage = new ResponseMessageTransfer_Customer_Service();
                 break;
             default:
                 throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在ResponseMessageFactory中没有对应的处理程序!", msgType), new ArgumentOutOfRangeException());
         }
         EntityHelper.FillEntityWithXml(responseMessage, doc);
     }
     catch (ArgumentException ex)
     {
         throw new WXException(string.Format("ResponseMessage转换出错!可能是MsgType不存在!,XML:{0}", doc.ToString()), ex);
     }
     return responseMessage;
 }
예제 #2
0
        public void GetResponseEntityTest()
        {
            {
                //Text
                ResponseMessageText exceptResult = new ResponseMessageText()
                                              {
                                                  ToUserName = "******",
                                                  FromUserName = "******",
                                                  CreateTime = DateTimeHelper.GetDateTimeFromXml(63497820384),
                                                  //MsgType = ResponseMsgType.Text,
                                                  Content = "文字信息",
                                                  FuncFlag = false
                                              };
                var result = ResponseMessageFactory.GetResponseEntity(xmlText) as ResponseMessageText;
                Assert.AreEqual(exceptResult.ToUserName, result.ToUserName);
                Assert.AreEqual(exceptResult.CreateTime, result.CreateTime);
                Assert.AreEqual(exceptResult.Content, result.Content);
            }

            {
                //Image
                ResponseMessageNews exceptResult = new ResponseMessageNews()
                                                     {
                                                         //Articles = new List<Article>(),
                                                         CreateTime = DateTimeHelper.GetDateTimeFromXml(63497821905),
                                                         FromUserName = "******",
                                                         ToUserName = "******",
                                                         FuncFlag = false,
                                                         //MsgType = ResponseMsgType.News
                                                     };
                var result = ResponseMessageFactory.GetResponseEntity(xmlNews) as ResponseMessageNews;
                Assert.AreEqual(exceptResult.ToUserName, result.ToUserName);
                Assert.AreEqual(exceptResult.CreateTime, result.CreateTime);
                Assert.AreEqual(2, result.ArticleCount);
                Assert.AreEqual(result.Articles.Count, result.ArticleCount);
            }

            //TODO:测试语音和视频类型

            {
                //Music
                ResponseMessageMusic exceptResult = new ResponseMessageMusic()
                                                        {
                                                            Music = new Music()
                                                                        {
                                                                            Title = "标题",
                                                                            Description = "说明",
                                                                            MusicUrl = "http://weixin.senparc.com/Content/music1.mp3",
                                                                            HQMusicUrl = ""
                                                                        },
                                                            CreateTime = DateTimeHelper.GetDateTimeFromXml(63497823450),
                                                            FromUserName = "******",
                                                            ToUserName = "******",
                                                            FuncFlag = false,
                                                            //MsgType = ResponseMsgType.Music
                                                        };
                var result = ResponseMessageFactory.GetResponseEntity(xmlMusic) as ResponseMessageMusic;
                Assert.AreEqual(exceptResult.ToUserName, result.ToUserName);
                Assert.AreEqual(exceptResult.CreateTime, result.CreateTime);
                //Assert.AreEqual(exceptResult.Music, result.Music);
            }
        }