微信公共平台开发3 .net

嗯,别的不说了现在开始接着上次http://www.cnblogs.com/QLJ1314/p/3838058.html  获取ACCESSTOKEN,开始吧,接下来我们就写发送文本消息吧。

   首先建立一个微信消息实体类。此原文出处:blog.csdn.net/hemeng1980/article/details/19503171

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 }

记着一定要和开发文档的格式一致,一定要把两个关系搞清楚呀。不然是实现不了效果的

原文地址:https://www.cnblogs.com/klsw/p/4790216.html