public void TestMethod1() { var str = @"<xml> <ToUserName><![CDATA[gh_38f4ef5d3064]]></ToUserName> <FromUserName><![CDATA[ocBLpjvEkE9-ml35Cl8lvi6hanYs]]></FromUserName> <CreateTime>1432173685</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[SCAN]]></Event> <EventKey><![CDATA[141]]></EventKey> <Ticket><![CDATA[gQHl7zoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3FIV212TmpsUUc0NVh3U2JuMXUwAAIEGDtdVQMEgDoJAA==]]></Ticket> </xml>"; UnicodeEncoding uniEncoding = new UnicodeEncoding(); byte[] bytes = uniEncoding.GetBytes(str); using (Stream stream = new MemoryStream(bytes)) { CustomMessageHandler messageHandler = new CustomMessageHandler( stream, _userProfile, _qrCodeRepo); messageHandler.Execute(); }; }
public ContentResult MiniPost(string id, string signature, string timestamp, string nonce, string echostr) { lock (_postingLock) { if (!_postingFlag) { _postingFlag = true; UserProfile userProfile = _userProfileRepository.Find(Specification<UserProfile>.Eval(e => e.UserCode == id)); if (userProfile != null) { if (!CheckSignature.Check(signature, timestamp, nonce, userProfile.WeixinToken)) { return Content("参数错误!"); } } else { return Content("参数错误!"); } var logfilename = userProfile.UserName; try { var messageHandler = new CustomMessageHandler(Request.InputStream, userProfile, signature, timestamp, nonce, _qrCodeRepository); //LogHelper.WriteLog(string.Format(@" signature: {0}timestamp: {1}nonce: {2}echostr: {3}requestdocument: {4}", signature, timestamp, nonce, echostr, // messageHandler.RequestDocument.ToString(), userProfile.UserName), // logfilename + "debug接收微信xml"); messageHandler.Execute(); //if (messageHandler.ResponseDocument != null) // LogHelper.WriteLog(messageHandler.ResponseDocument.ToString(), // logfilename + "debug返回微信xml"); _postingFlag = false; if (!CheckContent(messageHandler)) { return Content(""); } return Content(messageHandler.ResponseDocument.ToString());//v0.7- } catch (Exception ex) { LogHelper.WriteException(ex, logfilename); return Content(""); } } else return Content(""); } }
/// <summary> /// 之判断返回文字的时候的Content属性,因为Content如果为"",则会提示该公众号已停止服务 /// </summary> /// <param name="messageHandler"></param> /// <returns></returns> private bool CheckContent(CustomMessageHandler messageHandler) { if (messageHandler.ResponseDocument != null) { var xDocument = messageHandler.ResponseDocument; var element = xDocument.Descendants("Content").FirstOrDefault(); if (element != null) { if (!string.IsNullOrEmpty(element.Value)) { return true; } } else { return true; } } return false; }