Exemple #1
0
        /// <summary>
        /// 发送文字消息
        /// </summary>
        /// <param name="wx" />获取的收发者信息
        /// <param name="content" />内容
        /// <returns></returns>
        private string sendTextMessage(wxmessage wx, string content)
        {
            if (wx.MsgId != null && (!msgidlist.Contains(wx.MsgId)))
            {
                msgidlist.Add(wx.MsgId);
            }
            sss.InsertData("content:" + content);
            string res = "";

            switch (wx.MsgType)
            {
            case "text":
                res = string.Format(Message_Text,
                                    wx.FromUserName, wx.ToUserName, DateTime.Now.Ticks, "文字标题如下:\n" + content + "\n文本结尾引脚");
                break;

            case "image":
                res = string.Format(Message_Image,
                                    wx.FromUserName, wx.ToUserName, DateTime.Now.Ticks, wx.MsgType, wx.MediaId);
                break;

            case "voice":
                res = string.Format(Message_Voice,
                                    wx.FromUserName, wx.ToUserName, DateTime.Now.Ticks, wx.MsgType, wx.MediaId);
                break;

            default:
                res = string.Format(Message_Text,
                                    wx.FromUserName, wx.ToUserName, DateTime.Now.Ticks, content);
                break;
            }
            sss.InsertData("res:" + res);
            return(res);
        }
Exemple #2
0
        /// <summary>
        /// 获取和设置微信类中的信息
        /// </summary>
        /// <returns></returns>
        private wxmessage GetWxMessage()
        {
            wxmessage    wx  = new wxmessage();
            StreamReader str = new StreamReader(Request.InputStream, Encoding.UTF8);
            XmlDocument  xml = new XmlDocument();

            xml.Load(str);
            str.Close();
            str.Dispose();
            sss.InsertData("xml对象:" + Newtonsoft.Json.JsonConvert.SerializeObject(xml));
            if (xml.SelectSingleNode("xml").SelectSingleNode("MsgId") != null)
            {
                wx.MsgId = xml.SelectSingleNode("xml").SelectSingleNode("MsgId").InnerText;
            }


            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.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
            }
            else if (wx.MsgType.Trim() == "image")
            {
                wx.PicUrl  = xml.SelectSingleNode("xml").SelectSingleNode("PicUrl").InnerText;
                wx.MediaId = xml.SelectSingleNode("xml").SelectSingleNode("MediaId").InnerText;
            }
            else if (wx.MsgType.Trim() == "voice")
            {
                wx.MediaId = xml.SelectSingleNode("xml").SelectSingleNode("MediaId").InnerText;
            }
            else if (wx.MsgType.Trim() == "event")
            {
                wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
                wx.EventKey  = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;
            }
            return(wx);
        }
Exemple #3
0
        public ActionResult Post(string signature, string timestamp, string nonce, string echostr)
        {
            //Stream requestStream = System.Web.HttpContext.Current.Request.InputStream;
            //byte[] requestByte = new byte[requestStream.Length];
            //requestStream.Read(requestByte, 0, (int)requestStream.Length);
            //string requestStr = Encoding.UTF8.GetString(requestByte);
            //sss.InsertData("requestStr:" + requestStr);
            //return Content(requestStr);

            wxmessage wx  = GetWxMessage();
            string    res = "success";

            sss.InsertData("DDDD");
            if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")
            {
                //刚关注时的时间,用于欢迎词
                string content = "";
                content = "您好,欢迎关注郭顺开公众号";
                res     = sendTextMessage(wx, content);
                return(Content(res));
            }
            else if (!string.IsNullOrEmpty(wx.MsgType) && wx.MsgType.Trim() == "text")
            {
                res = sendTextMessage(wx, "接收到的消息text(:<a href=\"http://www.qq.com\">" + wx.Content + "</a>)");
                return(Content(res));
            }
            else if (!string.IsNullOrEmpty(wx.MsgType) && wx.MsgType.Trim() == "image" && !msgidlist.Contains(wx.MsgId))
            {
                res = sendTextMessage(wx, "接收到的消息image(:<a href=\"" + wx.PicUrl + "\">" + wx.MediaId + "</a>)");
                return(Content(res));
            }
            else if (!string.IsNullOrEmpty(wx.MsgType) && wx.MsgType.Trim() == "voice" && !msgidlist.Contains(wx.MsgId))
            {
                res = sendTextMessage(wx, "接收到的消息voice(:" + wx.MediaId + ")");
                return(Content(res));
            }
            return(Content(res));
        }