public ThirdPartyMessageHandler(Stream inputStream) { RequestDocument = XmlUtility.XmlUtility.Convert(inputStream);//转成XDocument //转成实体 RequestMessageBase requestMessage = null; InfoType infoType; try { infoType = InfoTypeHelper.GetRequestInfoType(RequestDocument); switch (infoType) { case InfoType.component_verify_ticket: requestMessage = new RequestMessageComponentVerifyTicket(); break; case InfoType.unauthorized: requestMessage = new RequestMessageUnauthorized(); break; default: throw new ArgumentOutOfRangeException(); } } catch (Exception ex) { throw; } requestMessage.FillEntityWithXml(RequestDocument); RequestMessage = requestMessage; }
//<?xml version="1.0" encoding="utf-8"?> //<xml> // <ToUserName><![CDATA[gh_a96a4a619366]]></ToUserName> // <FromUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></FromUserName> // <CreateTime>1357986928</CreateTime> // <MsgType><![CDATA[text]]></MsgType> // <Content><![CDATA[中文]]></Content> // <MsgId>5832509444155992350</MsgId> //</xml> /// <summary> /// 获取XDocument转换后的IRequestMessageBase实例。 /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常 /// </summary> /// <returns></returns> public static IRequestMessageBase GetRequestEntity(XDocument doc, PostModel postModel = null) { RequestMessageBase requestMessage = null; RequestInfoType infoType; try { infoType = InfoTypeHelper.GetRequestInfoType(doc); switch (infoType) { case RequestInfoType.component_verify_ticket: requestMessage = new RequestMessageComponentVerifyTicket(); break; case RequestInfoType.unauthorized: requestMessage = new RequestMessageUnauthorized(); break; default: throw new UnknownRequestMsgTypeException(string.Format("InfoType:{0} 在RequestMessageFactory中没有对应的处理程序!", infoType), new ArgumentOutOfRangeException()); //为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常 } EntityHelper.FillEntityWithXml(requestMessage, doc); } catch (ArgumentException ex) { throw new WeixinException(string.Format("RequestMessage转换出错!可能是InfoType不存在!,XML:{0}", doc.ToString()), ex); } return(requestMessage); }
//<?xml version="1.0" encoding="utf-8"?> //<xml> // <ToUserName><![CDATA[gh_a96a4a619366]]></ToUserName> // <FromUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></FromUserName> // <CreateTime>1357986928</CreateTime> // <MsgType><![CDATA[text]]></MsgType> // <Content><![CDATA[中文]]></Content> // <MsgId>5832509444155992350</MsgId> //</xml> /// <summary> /// 获取XDocument转换后的IRequestMessageBase实例。 /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常 /// </summary> /// <returns></returns> public static IRequestMessageBase GetRequestEntity(XDocument doc, PostModel postModel = null) { RequestMessageBase requestMessage = null; RequestInfoType infoType; try { infoType = InfoTypeHelper.GetRequestInfoType(doc); switch (infoType) { case RequestInfoType.component_verify_ticket: requestMessage = new RequestMessageComponentVerifyTicket(); break; case RequestInfoType.unauthorized: /* * <xml> * <AppId>第三方平台appid</AppId> * <CreateTime>1413192760</CreateTime> * <InfoType>unauthorized</InfoType> * <AuthorizerAppid>公众号appid</AuthorizerAppid> * </xml> */ requestMessage = new RequestMessageUnauthorized(); break; case RequestInfoType.authorized: /* * <xml> * <AppId>第三方平台appid</AppId> * <CreateTime>1413192760</CreateTime> * <InfoType>authorized</InfoType> * <AuthorizerAppid>公众号appid</AuthorizerAppid> * <AuthorizationCode>授权码(code)</AuthorizationCode> * <AuthorizationCodeExpiredTime>过期时间</AuthorizationCodeExpiredTime> * </xml> */ requestMessage = new RequestMessageAuthorized(); break; case RequestInfoType.updateauthorized: requestMessage = new RequestMessageUpdateAuthorized(); break; case RequestInfoType.notify_third_fasteregister: requestMessage = new RequestMessageThirdFasteRegister(); break; default: throw new UnknownRequestMsgTypeException(string.Format("InfoType:{0} 在RequestMessageFactory中没有对应的处理程序!", infoType), new ArgumentOutOfRangeException()); //为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常 } EntityHelper.FillEntityWithXml(requestMessage, doc); } catch (ArgumentException ex) { throw new WeixinException(string.Format("RequestMessage转换出错!可能是InfoType不存在!,XML:{0}", doc.ToString()), ex); } return(requestMessage); }