asp.net微信开发第六篇----高级群发(文本)
说到高级群发,微信的参考资料http://mp.weixin.qq.com/wiki/14/0c53fac3bdec3906aaa36987b91d64ea.html
首先我们先来讲解一下群发文本信息的过程,我个人开发程序是首先要有UI才能下手去写代码,界面如下,
看图我们也可以看出首先我们要获取该微信号本月还能群发几条信息,关于怎么计算,就是群发成功一条信息,就在本地数据库存储一条信息,用来计算条数,(这个我相信都会),大于4条就不能发送(这里我已经限制死了,因为服务号每月只能发送4条,多发送也没用,用户只能收到4条,除非使用预览功能,挨个发送,但预览功能也只能发送100次,或许可能使用开发者模式下群发信息可以多发送N次哦,因为我群发了两次之后,再进入到微信公众平台官网后台看到的居然还能群发4条,有点郁闷哦!),群发对象可选择为全部用户或分组用户,和由于节省群发次数,这里我就不测试群发文字信息了,具体参考如下代码:
/// <summary>
/// 绑定本月剩余群发条数
/// </summary>
private void BindMassCount()
{
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
//官方微信服务号每月只能群发4条信息,(订阅号每天1条)多余信息,将不会成功推送,这里已经设定为4
this.lbMassCounts.Text = ( - int.Parse(wxmaslist.Count.ToString())).ToString(); if (wxmaslist.Count >= )
{
this.LinkBtnSubSend.Enabled = false;
this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('群发信息已达上限!请下月初再试!')");
}
else
{
this.LinkBtnSubSend.Enabled = true;
this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('您确定要群发此条信息??')");
}
}
/// <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(); //设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(), System.Web.Caching.Cache.NoSlidingExpiration);
} string Access_tokento = Access_token.Substring(, Access_token.Length - ); 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(); //设置缓存的数据7000秒后过期
Cache.Insert("AllGroups_content", content, null, DateTime.Now.AddSeconds(), 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 = ; 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>
/// 选择群发对象类型,显示隐藏分组列表项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DDLMassType_SelectedIndexChanged(object sender, EventArgs e)
{
if (int.Parse(this.DDLMassType.SelectedValue.ToString()) > )
{
this.DDLGroupList.Visible = true;
}
else
{
this.DDLGroupList.Visible = false;
}
}
/// <summary>
/// 群发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkBtnSubSend_Click(object sender, EventArgs e)
{
//根据单选按钮判断类型,发送
///如果选择的是文本消息
if (this.RadioBtnList.SelectedValue.ToString().Equals(""))
{
if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入您要群发文本内容!');", true);
return;
}
if (this.txtwenben.InnerText.ToString().Trim().Length<)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('文本内容至少需要10个字符以上!');", true);
return;
} WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount(); if (wxmaslist.Count >= )
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群发消息数量已达上限!');", true);
return;
}
else
{ //如何群发类型为全部用户,根据openID列表群发给全部用户,订阅号不可用,服务号认证后可用
if (this.DDLMassType.SelectedValue.ToString().Equals(""))
{
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(); //设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(), System.Web.Caching.Cache.NoSlidingExpiration);
} string Access_tokento = Access_token.Substring(, Access_token.Length - ); string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento; ///群发POST数据示例如下:
// {
// "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(""))
{
//群发成功后,保存记录
WxMassInfo wmi = new WxMassInfo(); wmi.ImageUrl = "";
wmi.type = "文本";
wmi.contents = this.txtwenben.InnerText.ToString().Trim();
wmi.title = this.txtwenben.InnerText.ToString().Substring(, ) + "..."; if (this.DDLMassType.SelectedValue.ToString().Equals(""))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
} wmi.massStatus = "成功";//群发成功之后返回的状态码
wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID wmi.massDate = System.DateTime.Now.ToString(); int num = wms.AddWxMassInfo(wmi); if (num > )
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
return;
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", 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(); //设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(), System.Web.Caching.Cache.NoSlidingExpiration);
} string Access_tokento = Access_token.Substring(, Access_token.Length - ); string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento; ///群发POST数据示例如下:
// {
// "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(""))
{
//群发成功后,保存记录
WxMassInfo wmi = new WxMassInfo(); wmi.ImageUrl = "";
wmi.type = "文本";
wmi.contents = this.txtwenben.InnerText.ToString().Trim();
wmi.title = this.txtwenben.InnerText.ToString().Substring(, ) + "..."; if (this.DDLMassType.SelectedValue.ToString().Equals(""))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
} wmi.massStatus = "成功";//群发成功之后返回的状态码
wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID wmi.massDate = System.DateTime.Now.ToString(); int num = wms.AddWxMassInfo(wmi); if (num > )
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
return;
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", true);
return;
}
} }
}
//如果选择的是图文消息
if (this.RadioBtnList.SelectedValue.ToString().Equals(""))
{
if (String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请选择或新建图文素材再进行群发!');", true);
return;
} WxMassService wms = new WxMassService(); List<WxMassInfo> wxmaslist = wms.GetMonthMassCount(); if (wxmaslist.Count >= )
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群发消息数量已达上限!');", true);
return;
}
else
{ //如何群发类型为全部用户,根据openID列表群发给全部用户,订阅号不可用,服务号认证后可用
if (this.DDLMassType.SelectedValue.ToString().Equals(""))
{
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(); //设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(), System.Web.Caching.Cache.NoSlidingExpiration);
} string Access_tokento = Access_token.Substring(, Access_token.Length - ); string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento; ///群发POST数据示例如下:
// {
// "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(""))
{
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(""))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
} wmi.massStatus = "成功";//群发成功之后返回的状态码
wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID wmi.massDate = System.DateTime.Now.ToString(); int num = wms.AddWxMassInfo(wmi); if (num > )
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
return;
}
}
else
{
wmi.title = "";
wmi.contents = "";
wmi.ImageUrl = "";
wmi.type = "图文"; if (this.DDLMassType.SelectedValue.ToString().Equals(""))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
} wmi.massStatus = "成功";//群发成功之后返回的状态码
wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID wmi.massDate = System.DateTime.Now.ToString(); int num = wms.AddWxMassInfo(wmi); if (num > )
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!图文部分数据已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
return;
}
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", 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(); //设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(), System.Web.Caching.Cache.NoSlidingExpiration);
} string Access_tokento = Access_token.Substring(, Access_token.Length - ); string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento; ///群发POST数据示例如下:
// {
// "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(""))
{
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(""))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
} wmi.massStatus = "成功";//群发成功之后返回的状态码
wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID wmi.massDate = System.DateTime.Now.ToString(); int num = wms.AddWxMassInfo(wmi); if (num > )
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
return;
}
}
else
{
wmi.title = "";
wmi.contents = "";
wmi.ImageUrl = "";
wmi.type = "图文"; if (this.DDLMassType.SelectedValue.ToString().Equals(""))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
} wmi.massStatus = "成功";//群发成功之后返回的状态码
wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID wmi.massDate = System.DateTime.Now.ToString(); int num = wms.AddWxMassInfo(wmi); if (num > )
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!图文部分数据已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
return;
}
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", true);
return;
}
}
}
}
}
/// <summary>
/// 发送前预览
/// </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(); //设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(), System.Web.Caching.Cache.NoSlidingExpiration);
} string Access_tokento = Access_token.Substring(, Access_token.Length - ); string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=" + Access_tokento; ///如果选择的是文本消息
if (this.RadioBtnList.SelectedValue.ToString().Equals(""))
{
if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入您要发送预览的文本内容!');", true);
return;
}
if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号"))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true);
return;
}
//文本消息的json数据{
// "touser":"OPENID", 可改为对微信号预览,例如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(""))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true);
return;
}
}
//如果选择的是图文消息
if (this.RadioBtnList.SelectedValue.ToString().Equals(""))
{
if(String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请选择要预览的图文素材!');", true);
return;
}
if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号"))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true);
return;
}
//图文消息的json数据{
// "touser":"OPENID", 可改为对微信号预览,例如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(""))
{
Session["media_id"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true);
return;
} } }
关键部分,获取全部用户的openID并串联成字符串:
/// <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(); //设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(), System.Web.Caching.Cache.NoSlidingExpiration);
} string Access_tokento = Access_token.Substring(, Access_token.Length - ); 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(); //设置缓存的数据7000秒后过期
Cache.Insert("AllUserOpenList_content", content, null, DateTime.Now.AddSeconds(), 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 = ; i < totalnum; i++)
{
sb.Append('"');
sb.Append(jsonObj["data"]["openid"][i].ToString());
sb.Append('"');
sb.Append(",");
}
} return sb.Remove(sb.ToString().LastIndexOf(","),).ToString();
}
至此结束,下一章将继续讲解群发图文信息,因群发图文信息之前,需要先上传图文信息所需的素材,获取media_id,所以本章不做介绍,下一章将介绍新建单图文信息并群发。
asp.net微信开发第六篇----高级群发(文本)的更多相关文章
- asp.net微信开发第七篇----高级群发(图文)
上一篇介绍了如何群发文本消息,本篇将介绍如何群发图文信息,上传图文信息所需的素材,界面如下: 我们先看从素材库中获取图文素材的代码,界面: 素材列表,我是使用的repeater控件, 前台代码如下: ...
- asp.net微信开发第四篇----已关注用户管理
公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成.一次拉取调用最多拉取10000个关注者的OpenID,可以通过 ...
- asp.net微信开发第五篇----用户分组管理
上一篇已讲解到新建用户分组,移动用户到分组的功能,这一章主要讲解修改分组名称和删除分组 开发者可以使用接口,对公众平台的分组进行查询.创建.修改.删除等操作,也可以使用接口在需要时移动用户到某个分组. ...
- asp.net微信开发第三篇----自定义会话管理
和微信用户的沟通少不了,总觉得看起来微信官网后台管理中的会话回复消息有点呆板,所以我这里就自定义了一个会话管理功能,最终效果图如下: 因为我试使用富文本文件CKEDITOR来进行编写,你看到稳中可能会 ...
- asp.net微信开发第八篇----永久素材管理
除了3天就会失效的临时素材外,开发者有时需要永久保存一些素材,届时就可以通过本接口新增永久素材. 最近更新,永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用 ...
- asp.net微信开发第十篇----使用百度编辑器编辑图文消息,上传图片、微信视频
经过几天的资料收集,终于完成了该编辑器的图片上传,视频插入功能,视频插入功能主要借用了该编辑器的插入iframe功能,如原始插件图: 修改后的插件图如下(其中我隐藏掉了一些不需要使用的插件功能): 配 ...
- asp.net 微信开发(二)
本节我们主要讲解微信的调试: 前言:平时我们开发项目都是在本地就能进行项目的开发调试,但是在微信上就有难度了,因为微信的数据需要从微信服务器上面拿,所以就需要直接在网站上调试了,接下来就相关的一些个人 ...
- asp.net微信开发第二篇----消息应答
当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上. 请注意: 1.关于重试的消息排重,推荐使用msgid排重. 2.微信服务器在五秒内收不到响应会断掉连接 ...
- asp.net微信开发第一篇----开发者接入
在项目的根目录或者特定的文件夹内,创建一个ashx文件(一般处理程序文件),如图 public void ProcessRequest(HttpContext context) { context.R ...
随机推荐
- checkbox复选框全选批量删除
多选框全选实现批量删除 html代码 <body> <form action="" method="post" name="Form ...
- delphi 创建数据库配置文件(TIniFile)
一.有必要了解INI文件的结构: ;注释 [小节名] 关键字=值 ... ---- INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值. ---- 值的类型有三种:字符串 ...
- iOS开发——OC篇&常用关键字的使用与区别
copy,assign,strong,retain,weak,readonly,readwrite,nonatomic,atomic,unsafe_unretained的使用与区别 最近在学习iOS的 ...
- 自己动手做 UEStudio/UltraEdit 的语法高亮文件 (*.uew)
自己一直比较习惯用 UEStudio 来编写 C/C++ 文件,因为 Visual Studio 2010 实在太大了,我的 T400 都跑的费劲,所以一般我只用它来编译和调试.但是可惜的是 UESt ...
- -_-#【Canvas】measureText, translate, drawImage
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 舶来品P2P理财 能否成为“好声音”式好生意? 转
华股财经 2012年11月29日 10:20:02 来源:互联网 字号:T|T 文/本刊记者 王奇 有数据显示,目前国内已有2000余家P2P公司,2007年至2011年上半年,其整体融资规模由2 ...
- Jsp 中文乱码,解决
jsp 乱码 : The time on the server is 2016?2?7? ??10?45?32?. 在 jsp 中,用 jsp 语法添加 utf-8 字符集,可解决此问题 <%@ ...
- SRM468 - SRM469(1-250pt, 500pt)
SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...
- 基于JAVA的webVNC
jxpiInstall安装程序下载: http://sdlc-esd.sun.com/ESD6/JSCDL/jdk/7u60-b19/jxpiinstall.exe?AuthParam=1402208 ...
- Maven可继承的POM 元素
groupId :项目组 ID ,项目坐标的核心元素: version :项目版本,项目坐标的核心元素: description :项目的描述信息: organization :项目的组织信息: in ...