最近,項目這邊比較忙,沒來得及續寫,哎,先吐吐槽吧,在這個週六還得來上班,之後每一個週六多要上,一天的滋味真有點受不鳥呀。還不習慣ing...html
嗯,別的不說了如今開始接着上次http://www.cnblogs.com/QLJ1314/p/3838058.html 獲取ACCESSTOKEN,開始吧,接下來咱們就寫發送文本消息吧。微信
首先創建一個微信消息實體類。此原文出處:blog.csdn.net/hemeng1980/article/details/19503171ide
1 class wxmessage 2 { 3 public string FromUserName { get; set; } 消息發送方微信號 4 public string ToUserName { get; set; } 消息接收方微信號,通常爲公衆平臺帳號微信號 5 public string MsgType { get; set; } 信息類型 6 public string EventName { get; set; } 7 public string Content { get; set; } 信息內容 8 public string EventKey { get; set; } 9 }
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 wxmessage wx = GetWxMessage(); 4 string res = ""; 5 6 if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe") 7 {//剛關注時的時間,用於歡迎詞 8 string content = ""; 9 content = "你好,感謝你關注QLJ1314博客"; 10 res = sendTextMessage(wx, content); 11 } 12 else 13 { 14 if (wx.MsgType == "text" && wx.Content == "你好") 15 { 16 res = sendTextMessage(wx, "你好,感謝你關注QLJ1314博客!"); 17 } 18 else 19 { 20 res = sendTextMessage(wx, "這個我也沒碰見過,正在向微信客服反應此事,請耐心等待或者能夠直接打10086!"); 21 } 22 } 23 24 Response.Write(res); 25 } 26 //獲取用戶基本信息 27 private wxmessage GetWxMessage() 28 { 29 wxmessage wx = new wxmessage(); 30 StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8); 31 XmlDocument xml = new XmlDocument(); 32 xml.Load(str); 33 wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText; 34 wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText; 35 wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText; 36 if (wx.MsgType.Trim() == "text") 37 { 38 wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText; 39 } 40 if (wx.MsgType.Trim() == "event") 41 { 42 wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText; 43 } 44 45 46 return wx; 47 } 48 49 /// <summary> 50 /// 發送文字消息 51 /// </summary> 52 /// <param name="wxCont">獲取的收發者信息 53 /// <param name="content">內容 54 /// <returns>string </returns> 55 private string sendTextMessage(wxmessage wxCont, string content) 56 { 57 string res = string.Format(@" ", 58 wx.FromUserName, wx.ToUserName, DateTime.Now, content); 59 return res; 60 }
記着必定要和開發文檔的格式一致,必定要把兩個關係搞清楚呀。否則是實現不了效果的。哎 行了下班走人,下週再續吧。url