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

Response.Write(resxml);
            }
            if (MsgType.InnerText == "image")//如果消息类型为图片消息
            {
                var model = new
                {
                    ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText,
                    FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText,
                    CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText,
                    MsgType = MsgType.InnerText,
                    PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText,
                    MsgId = rootElement.SelectSingleNode("MsgId").InnerText
                };
                resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[欢迎您的光临!]]></Title><Description><![CDATA[非常感谢您的关注!]]></Description><PicUrl><![CDATA[]]></PicUrl><Url><![CDATA[]]></Url></item></Articles><FuncFlag>0</FuncFlag></xml>";
                Response.Write(resxml);
            }
            else//如果是其余的消息类型
            {
                var model = new
                {
                    ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText,
                    FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText,
                    CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText,
                };
                resxml += "<xml><ToUserName><![CDATA[" + model.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + model.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[亲,感谢您对我的关注,有事请留言,我会及时回复你的哦。]]></Content><FuncFlag>0</FuncFlag></xml>";
                Response.Write(resxml);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        Response.End();

}
/// <summary>
/// datetime转换成unixtime
/// </summary>
/// <param></param>
/// <returns></returns>
private int ConvertDateTimeInt(System.DateTime time)
{
        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
        return (int)(time - startTime).TotalSeconds;
}
/// <summary>
/// 写日志(用于跟踪),可以将想打印出的内容计入一个文本文件里面,便于测试
/// </summary>
public static void WriteLog(string strMemo, HttpServerUtility server)
{
        string filename = server.MapPath("/logs/log.txt");//在网站项目中建立一个文件夹命名logs(然后在文件夹中随便建立一个web页面文件,避免网站在发布到服务器之后看不到预定文件)
        if (!Directory.Exists(server.MapPath("//logs//")))
            Directory.CreateDirectory("//logs//");
        StreamWriter sr = null;
        try
        {
            if (!File.Exists(filename))
            {
                sr = File.CreateText(filename);
            }
            else
            {
                sr = File.AppendText(filename);
            }
            sr.WriteLine(strMemo);
        }
        catch
        {
        }
        finally
        {
            if (sr != null)
                sr.Close();
        }
}

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

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