微信开发之(四)获取关注后的图文推送信息

当我们关注公众号后,我们可以推送一篇图文里面可以有相关的介绍和活动及欢迎的话:

    //请求处理的方法
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string postString = string.Empty; if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } if (!string.IsNullOrEmpty(postString)) { ResponseXML(postString);//返回给微信用户信息 // ReturnMessage(postString); } } } /// <summary> /// 发送关注时的图文消息 /// </summary> /// <param name="requestXML"></param> private void SendWelComeMsg(RequestXML requestXML) { String responseContent = String.Empty; string newdate = DateTime.Now.Subtract(new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds.ToString(); //图片的链接地址 string PUrlfileName = "http://mmbiz.qpic.cn/mmbiz/7iaiaRJE9s3HAbvxtjqbE85wqmektib1xnwdl8dniaFuK5BMnVhehWzTlK2zW0YVzFfLKjexQfgj12SJcwicGSFOyvw/640?wx_fmt=jpeg&wxfrom=5"; responseContent = string.Format(ResponseXMl.Message_News_Main, requestXML.FromUserName, requestXML.ToUserName, newdate, "1", //参数分别代表发送图文的标题,简介,以及图文的链接地址 string.Format(ResponseXMl.Message_News_Item, "欢迎关注我的测试号", "2016年新的机遇和挑战...", PUrlfileName, "http://mp.weixin.qq.com/s?__biz=MzA4NjIwMTg5Nw==&tempkey=1D1FlQbenM0RdbGL7jPebIYV7OWGdyEY8DClNjaJHJ3yoGh1t3KfoVvK2FGXjgeuTMemMzu4GASlT2p4yyPT4oZp8bVWH1kF3BxYJGHGHnPpcr%2Bb8zdejjshCRSr8V18HPS9gh%2Fa0wKarCp%2B2mYOEA%3D%3D&#rd")); HttpContext.Current.Response.ContentType = "text/xml"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); HttpContext.Current.Response.End(); }
/// <summary> /// 返回图文消息主体 /// </summary> /// public static string Message_News_Main { get { return @"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>{3}</ArticleCount> <Articles> {4} </Articles> </xml> "; } } /// <summary> /// 返回图文消息项 /// </summary> public static string Message_News_Item { get { return @"<item> <Title><![CDATA[{0}]]></Title> <Description><![CDATA[{1}]]></Description> <PicUrl><![CDATA[{2}]]></PicUrl> <Url><![CDATA[{3}]]></Url> </item>"; }
/// <summary> /// 响应请求的信息 /// </summary> /// <param name="requestXML"></param> private void ResponseMsg(RequestXML requestXML) { string MsgType = requestXML.MsgType; try { //根据消息类型判断发送何种类型消息 switch (MsgType) { case "text": SendTextCase(requestXML);//发送文本消息 break; case "event"://发送事件消息 if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("subscribe")) { SendWelComeMsg(requestXML);//关注时返回的图文消息 } else if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("CLICK")) { SendEventMsg(requestXML);//发送事件消息 } break; case "voice": SendVoiceMsg(requestXML);//发送语音消息 break; case "location"://发送位置消息 SendMapMsg(requestXML); break; default: break; } } catch (Exception ex) { HttpContext.Current.Response.Write(ex.ToString()); } } }
/// <summary> /// 获取用户发送的消息 /// </summary> /// <param name="postString"></param> private void ResponseXML(string postString) { //使用XMLDocument加载信息结构 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(postString); XmlElement rootElement = xmlDoc.DocumentElement;//获取文档的根 XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); //获取消息的文本类型 RequestXML requestXML = new RequestXML();//声明实例,获取各个属性并赋值 requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;//公众号 requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;//用户 requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;//创建时间 requestXML.MsgType = MsgType.InnerText;//消息类型 ///对消息的不同类型进行赋值 if (requestXML.MsgType == "text") { //赋值文本信息内容 requestXML.Content = rootElement.SelectSingleNode("Content").InnerText; } if (requestXML.MsgType.Trim() == "location") { ///赋值地理位置纬度,经度,地图缩放比例,地理位置说明 requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText; requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText; requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText; requestXML.Label = rootElement.SelectSingleNode("Label").InnerText; } if (requestXML.MsgType.Trim().ToLower() == "event") { ///赋值事件名称和事件key值 requestXML.EventName = rootElement.SelectSingleNode("Event").InnerText; requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; } if (requestXML.MsgType.Trim().ToLower() == "voice") { ///赋值语音识别结果,赋值之前一定要记得在开发者模式下,把语音识别功能开启,否则获取不到 requestXML.Recognition = rootElement.SelectSingleNode("Recognition").InnerText; } ResponseMsg(requestXML);//调用图文信息 }

 

微信开发之(四)获取关注后的图文推送信息

上一篇:Asp.Net微信登录-手机网站APP应用


下一篇:Android 支付宝以及微信支付快速接入流程