这篇文章详解微信公众平台开发之发送文本消息.net代码解析方法
.net实现微信公共服务平台开发中的发送文本消息功能,具体内容如下
首先建立一个微信消息类。
class wxmessage
{
public string fromusername { get; set; }
public string tousername { get; set; }
public string msgtype { get; set; }
public string eventname { get; set; }
public string content { get; set; }
public string eventkey { get; set; }
}
后台代码如下:
protected void page_load(object sender, eventargs e)
{
wxmessage wx = getwxmessage();
string res = "";
if (!string.isnullorempty(wx.eventname) && wx.eventname.trim() == "subscribe")
{//刚关注时的时间,用于欢迎词
string content = "";
content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”";
res = sendtextmessage(wx, content);
}
else
{
if (wx.msgtype == "text" && wx.content == "你好")
{
res = sendtextmessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");
}
else
{
res = sendtextmessage(wx, "你好,未能识别消息!");
}
}
response.write(res);
}
private wxmessage getwxmessage()
{
wxmessage wx = new wxmessage();
streamreader str = new streamreader(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.content = xml.selectsinglenode("xml").selectsinglenode("content").innertext;
}
if (wx.msgtype.trim() == "event")
{
wx.eventname = xml.selectsinglenode("xml").selectsinglenode("event").innertext;
}
return wx;
}
///
/// 发送文字消息
///
/// 获取的收发者信息
/// 内容
///
private string sendtextmessage(wxmessage wx, string content)
{
string res = string.format(@" ",
wx.fromusername, wx.tousername, datetime.now, content);
return res;
}
以上就是详解微信公众平台开发之发送文本消息.net代码解析方法的详细内容。