asp.net微信開發(fā)(高級(jí)群發(fā)文本)
首先我們先來(lái)講解一下群發(fā)文本信息的過(guò)程,我個(gè)人開發(fā)程序是首先要有UI才能下手去寫代碼,界面如下,


看圖我們也可以看出首先我們要獲取該微信號(hào)本月還能群發(fā)幾條信息,關(guān)于怎么計(jì)算,就是群發(fā)成功一條信息,就在本地?cái)?shù)據(jù)庫(kù)存儲(chǔ)一條信息,用來(lái)計(jì)算條數(shù),(這個(gè)我相信都會(huì)),大于4條就不能發(fā)送(這里我已經(jīng)限制死了,因?yàn)榉?wù)號(hào)每月只能發(fā)送4條,多發(fā)送也沒(méi)用,用戶只能收到4條,除非使用預(yù)覽功能,挨個(gè)發(fā)送,但預(yù)覽功能也只能發(fā)送100次,或許可能使用開發(fā)者模式下群發(fā)信息可以多發(fā)送N次哦,因?yàn)槲胰喊l(fā)了兩次之后,再進(jìn)入到微信公眾平臺(tái)官網(wǎng)后臺(tái)看到的居然還能群發(fā)4條,有點(diǎn)郁悶哦?。?,群發(fā)對(duì)象可選擇為全部用戶或分組用戶,和由于節(jié)省群發(fā)次數(shù),這里我就不測(cè)試群發(fā)文字信息了,具體參考如下代碼:
綁定本月剩余群發(fā)條數(shù)
/// <summary>
/// 綁定本月剩余群發(fā)條數(shù)
/// </summary>
private void BindMassCount()
{
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
//官方微信服務(wù)號(hào)每月只能群發(fā)4條信息,(訂閱號(hào)每天1條)多余信息,將不會(huì)成功推送,這里已經(jīng)設(shè)定為4
this.lbMassCounts.Text = (4 - int.Parse(wxmaslist.Count.ToString())).ToString();
if (wxmaslist.Count >= 4)
{
this.LinkBtnSubSend.Enabled = false;
this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('群發(fā)信息已達(dá)上限!請(qǐng)下月初再試!')");
}
else
{
this.LinkBtnSubSend.Enabled = true;
this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('您確定要群發(fā)此條信息??')");
}
}
綁定分組列表
/// <summary>
/// 綁定分組列表
/// </summary>
private void BindGroupList()
{
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string jsonres = "";
string content = Cache["AllGroups_content"] as string;
if (content == null)
{
jsonres = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + Access_tokento;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
content = reader.ReadToEnd();
reader.Close();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("AllGroups_content", content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
//使用前需要引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(content);
int groupsnum = jsonObj["groups"].Count();
this.DDLGroupList.Items.Clear();//清除
for (int i = 0; i < groupsnum; i++)
{
this.DDLGroupList.Items.Add(new ListItem(jsonObj["groups"][i]["name"].ToString() + "(" + jsonObj["groups"][i]["count"].ToString() + ")", jsonObj["groups"][i]["id"].ToString()));
}
}
/// <summary>
/// 選擇群發(fā)對(duì)象類型,顯示隱藏分組列表項(xiàng)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DDLMassType_SelectedIndexChanged(object sender, EventArgs e)
{
if (int.Parse(this.DDLMassType.SelectedValue.ToString()) > 0)
{
this.DDLGroupList.Visible = true;
}
else
{
this.DDLGroupList.Visible = false;
}
}
群發(fā)
/// <summary>
/// 群發(fā)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkBtnSubSend_Click(object sender, EventArgs e)
{
//根據(jù)單選按鈕判斷類型,發(fā)送
///如果選擇的是文本消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("0"))
{
if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('請(qǐng)輸入您要群發(fā)文本內(nèi)容!');", true);
return;
}
if (this.txtwenben.InnerText.ToString().Trim().Length<10)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('文本內(nèi)容至少需要10個(gè)字符以上!');", true);
return;
}
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
if (wxmaslist.Count >= 4)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群發(fā)消息數(shù)量已達(dá)上限!');", true);
return;
}
else
{
//如何群發(fā)類型為全部用戶,根據(jù)openID列表群發(fā)給全部用戶,訂閱號(hào)不可用,服務(wù)號(hào)認(rèn)證后可用
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
StringBuilder sbs = new StringBuilder();
sbs.Append(GetAllUserOpenIDList());
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
///群發(fā)POST數(shù)據(jù)示例如下:
// {
// "touser":[
// "OPENID1",
// "OPENID2"
// ],
// "msgtype": "text",
// "text": { "content": "hello from boxer."}
//}
string postData = "{\"touser\":[" + sbs.ToString() +
"],\"msgtype\":\"text\",\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
"\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
//群發(fā)成功后,保存記錄
WxMassInfo wmi = new WxMassInfo();
wmi.ImageUrl = "";
wmi.type = "文本";
wmi.contents = this.txtwenben.InnerText.ToString().Trim();
wmi.title = this.txtwenben.InnerText.ToString().Substring(0, 10) + "...";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功!!!數(shù)據(jù)保存失敗!');", true);
return;
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)提交失敗??!');", true);
return;
}
}
else
{
string group_id = this.DDLGroupList.SelectedValue.ToString();
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
///群發(fā)POST數(shù)據(jù)示例如下:
// {
// "filter":{
// "is_to_all":false
// "group_id":"2"
// },
// "text":{
// "content":"CONTENT"
// },
// "msgtype":"text"
//}
//}
string postData = "{\"filter\":{\"is_to_all\":\"false\"\"group_id\":\"" + group_id +
"\"},\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
"\"},\"msgtype\":\"text\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
//群發(fā)成功后,保存記錄
WxMassInfo wmi = new WxMassInfo();
wmi.ImageUrl = "";
wmi.type = "文本";
wmi.contents = this.txtwenben.InnerText.ToString().Trim();
wmi.title = this.txtwenben.InnerText.ToString().Substring(0, 10) + "...";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失?。?);", true);
return;
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)提交失?。。?);", true);
return;
}
}
}
}
//如果選擇的是圖文消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("1"))
{
if (String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('請(qǐng)選擇或新建圖文素材再進(jìn)行群發(fā)!');", true);
return;
}
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
if (wxmaslist.Count >= 4)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群發(fā)消息數(shù)量已達(dá)上限!');", true);
return;
}
else
{
//如何群發(fā)類型為全部用戶,根據(jù)openID列表群發(fā)給全部用戶,訂閱號(hào)不可用,服務(wù)號(hào)認(rèn)證后可用
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
StringBuilder sbs = new StringBuilder();
sbs.Append(GetAllUserOpenIDList());
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
///群發(fā)POST數(shù)據(jù)示例如下:
// {
// "touser":[
// "OPENID1",
// "OPENID2"
// ],
// "mpnews":{
// "media_id":"123dsdajkasd231jhksad"
// },
// "msgtype":"mpnews"
//}
string postData = "{\"touser\":[" + sbs.ToString() +
"],\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() +
"\"},\"msgtype\":\"mpnews\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
Session["media_id"] = null;
WxMassInfo wmi = new WxMassInfo();
if (Session["wmninfo"] != null)
{
WxMpNewsInfo wmninfo = Session["wmninfo"] as WxMpNewsInfo;
wmi.title = wmninfo.title.ToString();
wmi.contents = wmninfo.contents.ToString();
wmi.ImageUrl = wmninfo.ImageUrl.ToString();
wmi.type = "圖文";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失??!');", true);
return;
}
}
else
{
wmi.title = "";
wmi.contents = "";
wmi.ImageUrl = "";
wmi.type = "圖文";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!圖文部分?jǐn)?shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失??!');", true);
return;
}
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)提交失?。?!');", true);
return;
}
}
else
{
//根據(jù)分組進(jìn)行群發(fā),訂閱號(hào)和服務(wù)號(hào)認(rèn)證后均可用
string group_id = this.DDLGroupList.SelectedValue.ToString();
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
///群發(fā)POST數(shù)據(jù)示例如下:
// {
// "filter":{
// "is_to_all":false
// "group_id":"2"
// },
// "mpnews":{
// "media_id":"123dsdajkasd231jhksad"
// },
// "msgtype":"mpnews"
//}
string postData = "{\"filter\":{\"is_to_all\":\"false\"\"group_id\":\""+group_id+
"\"},\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() +
"\"},\"msgtype\":\"mpnews\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
Session["media_id"] = null;
WxMassInfo wmi = new WxMassInfo();
if (Session["wmninfo"] != null)
{
WxMpNewsInfo wmninfo = Session["wmninfo"] as WxMpNewsInfo;
wmi.title = wmninfo.title.ToString();
wmi.contents = wmninfo.contents.ToString();
wmi.ImageUrl = wmninfo.ImageUrl.ToString();
wmi.type = "圖文";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失??!');", true);
return;
}
}
else
{
wmi.title = "";
wmi.contents = "";
wmi.ImageUrl = "";
wmi.type = "圖文";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!圖文部分?jǐn)?shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失?。?);", true);
return;
}
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)提交失?。?!');", true);
return;
}
}
}
}
}
發(fā)送前預(yù)覽
/// <summary>
/// 發(fā)送前預(yù)覽
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkBtnSendPreview_Click(object sender, EventArgs e)
{
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=" + Access_tokento;
///如果選擇的是文本消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("0"))
{
if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('請(qǐng)輸入您要發(fā)送預(yù)覽的文本內(nèi)容!');", true);
return;
}
if (this.txttoUserName.Value.ToString().Trim().Equals("請(qǐng)輸入用戶微信號(hào)"))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('請(qǐng)輸入接收消息的用戶微信號(hào)!');", true);
return;
}
//文本消息的json數(shù)據(jù){
// "touser":"OPENID", 可改為對(duì)微信號(hào)預(yù)覽,例如towxname:zhangsan
// "text":{
// "content":"CONTENT"
// },
// "msgtype":"text"
//}
string postData = "{\"towxname\":\"" + this.txttoUserName.Value.ToString() +
"\",\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
"\"},\"msgtype\":\"text\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('發(fā)送預(yù)覽成功??!');", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('發(fā)送預(yù)覽失敗??!');", true);
return;
}
}
//如果選擇的是圖文消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("1"))
{
if(String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('請(qǐng)選擇要預(yù)覽的圖文素材!');", true);
return;
}
if (this.txttoUserName.Value.ToString().Trim().Equals("請(qǐng)輸入用戶微信號(hào)"))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('請(qǐng)輸入接收消息的用戶微信號(hào)!');", true);
return;
}
//圖文消息的json數(shù)據(jù){
// "touser":"OPENID", 可改為對(duì)微信號(hào)預(yù)覽,例如towxname:zhangsan
// "mpnews":{
// "media_id":"123dsdajkasd231jhksad"
// },
// "msgtype":"mpnews"
//}
string postData = "{\"towxname\":\"" + this.txttoUserName.Value.ToString() +
"\",\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() +
"\"},\"msgtype\":\"mpnews\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
Session["media_id"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('發(fā)送預(yù)覽成功?。?);", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('發(fā)送預(yù)覽失?。?!');", true);
return;
}
}
}
關(guān)鍵部分,獲取全部用戶的openID并串聯(lián)成字符串:
/// <summary>
/// 獲取所有微信用戶的OpenID
/// </summary>
/// <returns></returns>
protected string GetAllUserOpenIDList()
{
StringBuilder sb = new StringBuilder();
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string jsonres = "";
string content = Cache["AllUserOpenList_content"] as string;
if (content == null)
{
jsonres = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + Access_tokento;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
content = reader.ReadToEnd();
reader.Close();
//設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
Cache.Insert("AllUserOpenList_content", content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
//使用前需要引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(content);
if (jsonObj.ToString().Contains("count"))
{
int totalnum = int.Parse(jsonObj["count"].ToString());
for (int i = 0; i < totalnum; i++)
{
sb.Append('"');
sb.Append(jsonObj["data"]["openid"][i].ToString());
sb.Append('"');
sb.Append(",");
}
}
return sb.Remove(sb.ToString().LastIndexOf(","),1).ToString();
}
本文已被整理到了《ASP.NET微信開發(fā)教程匯總》,歡迎大家學(xué)習(xí)閱讀。
至此結(jié)束,下一章將繼續(xù)講解群發(fā)圖文信息,因群發(fā)圖文信息之前,需要先上傳圖文信息所需的素材,獲取media_id,所以本章不做介紹,下一章將介紹新建單圖文信息并群發(fā),希望大家喜歡。
- 微信公眾平臺(tái)開發(fā)——群發(fā)信息
- php微信公眾平臺(tái)開發(fā)之微信群發(fā)信息
- php微信高級(jí)接口群發(fā) 多客服
- C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口
- asp.net微信開發(fā)(高級(jí)群發(fā)圖文)
- php實(shí)現(xiàn)微信公眾號(hào)無(wú)限群發(fā)
- C#實(shí)現(xiàn)微信公眾號(hào)群發(fā)消息(解決一天只能發(fā)一次的限制)實(shí)例分享
- php微信公眾號(hào)開發(fā)(4)php實(shí)現(xiàn)自定義關(guān)鍵字回復(fù)
- php微信公眾號(hào)開發(fā)(3)php實(shí)現(xiàn)簡(jiǎn)單微信文本通訊
- 微信公眾號(hào)模板消息群發(fā)php代碼示例
相關(guān)文章
asp.net 獲取系統(tǒng)中參數(shù)的實(shí)現(xiàn)代碼
asp.net 獲取系統(tǒng)中參數(shù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12
服務(wù)器安全狗導(dǎo)致ASP.NET網(wǎng)站運(yùn)行出錯(cuò)的一個(gè)案例
這篇文章主要介紹了服務(wù)器安全狗導(dǎo)致ASP.NET網(wǎng)站運(yùn)行出錯(cuò)的一個(gè)案例,最后一并給出了解決方法,需要的朋友可以參考下2014-08-08
使用最小?WEB?API?實(shí)現(xiàn)文件上傳的Swagger支持
這篇文章主要介紹了使用最小?WEB?API?實(shí)現(xiàn)文件上傳Swagger支持,我們使用最小?WEB?API?實(shí)現(xiàn)文件上傳功能,雖然客戶端訪問(wèn)是正常的,但是當(dāng)打開?Swagger?頁(yè)面時(shí),沒(méi)法使用?Swagger?頁(yè)面測(cè)試,下面就來(lái)一篇支持Swagger的,需要的小伙伴可以參考一下2022-02-02
ASP.Net中防止刷新自動(dòng)觸發(fā)事件的解決方案
ASP.Net中防止刷新自動(dòng)觸發(fā)事件的解決方案...2007-03-03
Global.asax取物理路徑/取絕對(duì)路徑具體方法
本文章來(lái)給大家簡(jiǎn)單介紹利用Global.asax取物理路徑和取絕對(duì)路徑代碼,有需要了解的朋友可參考參考2013-08-08

