微信公眾平臺開發(fā)之發(fā)送文本消息.Net代碼解析
.Net實(shí)現(xiàn)微信公共服務(wù)平臺開發(fā)中的發(fā)送文本消息功能,具體內(nèi)容如下
首先建立一個(gè)微信消息類。
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")
{//剛關(guān)注時(shí)的時(shí)間,用于歡迎詞
string content = "";
content = "/:rose歡迎北京永杰友信科技有限公司/:rose\n直接回復(fù)“你好”";
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;
}
///
/// 發(fā)送文字消息
///
/// 獲取的收發(fā)者信息
/// 內(nèi)容
///
private string sendTextMessage(wxmessage wx, string content)
{
string res = string.Format(@" ",
wx.FromUserName, wx.ToUserName, DateTime.Now, content);
return res;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net 實(shí)現(xiàn)靜態(tài)頁面累加訪問量的三種方式
asp.net 實(shí)現(xiàn)靜態(tài)頁面累加訪問量的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-03-03
.Net?Core?3.1?Web?API基礎(chǔ)知識詳解(收藏)
這篇文章主要介紹了.Net?Core?3.1?Web?API基礎(chǔ)知識,本文內(nèi)容篇幅有點(diǎn)長,大家耐心閱讀,此文結(jié)合示例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-04-04
ASP.NET Core3.1 Ocelot負(fù)載均衡的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core3.1 Ocelot負(fù)載均衡的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
MVC4制作網(wǎng)站教程第二章 部分用戶功能實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了MVC4制作網(wǎng)站教程,部分用戶功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
.NET Core 2.1中HttpClientFactory的最佳實(shí)踐記錄
這篇文章主要給大家介紹了關(guān)于.NET Core 2.1中HttpClientFactory的最佳實(shí)踐記錄,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用.net core2.1具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-08-08
ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理
ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理...2006-09-09
.Net Web Api中利用FluentValidate進(jìn)行參數(shù)驗(yàn)證的方法
最近在做Web API,用到了流式驗(yàn)證,就簡單的說說這個(gè)流式驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于.Net Web Api中利用FluentValidate進(jìn)行參數(shù)驗(yàn)證的相關(guān)資料,,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
asp.net sql 數(shù)據(jù)庫處理函數(shù)命令
asp.net sql 數(shù)據(jù)庫處理函數(shù)命令 ,需要的朋友可以參考下。2009-10-10
Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼
Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼,學(xué)習(xí)asp.net mvc框架的朋友可以參考下。2010-10-10

