Пример #1
0
        /// <summary>
        /// 发送文字消息(主动)
        /// </summary>
        /// <param name="user"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public string sendTextMessage1(Model.weixin.wxmessage wx, string content)
        {
            string strJson = "{\"touser\": \"" + wx + "\",\"msgtype\": \"text\", \"text\": {\"content\":" + content + "}}";
            string res     = Post("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Token + "", strJson);

            return(GetErrorMsg(res).errmsg);
        }
Пример #2
0
        /// <summary>
        /// 发送文字消息(被动)
        /// </summary>
        /// <param name="wx">获取的收发者信息
        /// <param name="content">内容
        /// <returns></returns>
        public string sendTextMessage(Model.weixin.wxmessage wx, string content)
        {
            if (content == "")
            {
                return("");
            }
            StringBuilder sb = new StringBuilder();

            sb.Append("<xml>");
            sb.Append("<ToUserName><![CDATA[" + wx.FromUserName + "]]></ToUserName>");
            sb.Append("<FromUserName><![CDATA[" + number + "]]></FromUserName>");
            TimeSpan ts   = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string   time = Convert.ToInt64(ts.TotalSeconds).ToString();

            sb.Append("<CreateTime>" + time + "</CreateTime>");
            sb.Append("<MsgType><![CDATA[text]]></MsgType>");
            sb.Append("<Content><![CDATA[" + content + "]]></Content>");
            sb.Append("</xml>");
            return(sb.ToString());
        }
Пример #3
0
        /// <summary>
        /// 获得微信消息
        /// </summary>
        /// <returns></returns>
        public Model.weixin.wxmessage GetWxMessage()
        {
            Model.weixin.wxmessage wx  = new Model.weixin.wxmessage();
            StreamReader           str = new StreamReader(System.Web.HttpContext.Current.Request.InputStream, System.Text.Encoding.UTF8);
            XmlDocument            xml = new XmlDocument();

            xml.Load(str);
            wx.ToUserName   = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
            wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
            wx.MsgType      = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
            if (wx.MsgType.Trim() == "text")
            {
                wx.MsgId   = xml.SelectSingleNode("xml").SelectSingleNode("MsgId").InnerText;
                wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
            }
            if (wx.MsgType.Trim() == "event")
            {
                wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
                wx.EventKey  = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;
            }
            return(wx);
        }