.net开发微信公众平台实例教程(2)

/// <summary>
/// 获取post返回来的数据
/// </summary>
/// <returns></returns>
private string PostInput()
{
        Stream s = System.Web.HttpContext.Current.Request.InputStream;
        byte[] b = new byte[s.Length];
        s.Read(b, 0, (int)s.Length);
        return Encoding.UTF8.GetString(b);
}

/// <summary>
///返回微信信息结果
/// </summary>
/// <param></param>
private void ResponseMsg(string weixinXML)
{
        try
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(weixinXML);//读取XML字符串
            XmlElement rootElement = doc.DocumentElement;

XmlNode MsgType = rootElement.SelectSingleNode("MsgType");//获取字符串中的消息类型

string resxml = "";
            if (MsgType.InnerText == "text")//如果消息类型为文本消息
            {
                var model = new
                {
                    ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText,
                    FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText,
                    CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText,
                    MsgType = MsgType.InnerText,
                    Content = rootElement.SelectSingleNode("Content").InnerText,
                    MsgId = rootElement.SelectSingleNode("MsgId").InnerText
                };
                resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime>";
                if (!string.IsNullOrEmpty(model.Content))//如果接收到消息
                {
                    if (model.Content.Contains(" 你好") || model.Content.Contains(" 好") || model.Content.Contains("hi") || model.Content.Contains("hello"))// 你好
                    {
                        resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你好,有事请留言,偶会及时回复你的。]]></Content><FuncFlag>0</FuncFlag></xml>";
                    }

}

else//没有接收到消息
                {
                    resxml += "<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[亲,感谢您对我的关注,有事请留言。]]></Content><FuncFlag>0</FuncFlag></xml>";
                }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzgsw.html