热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

asp.net微信开发(高级群发文本)

这篇文章主要介绍了asp.net微信开发中有关高级群发文本的相关内容,需要的朋友可以参考下

首先我们先来讲解一下群发文本信息的过程,我个人开发程序是首先要有UI才能下手去写代码,界面如下,

 

看图我们也可以看出首先我们要获取该微信号本月还能群发几条信息,关于怎么计算,就是群发成功一条信息,就在本地数据库存储一条信息,用来计算条数,(这个我相信都会),大于4条就不能发送(这里我已经限制死了,因为服务号每月只能发送4条,多发送也没用,用户只能收到4条,除非使用预览功能,挨个发送,但预览功能也只能发送100次,或许可能使用开发者模式下群发信息可以多发送N次哦,因为我群发了两次之后,再进入到微信公众平台官网后台看到的居然还能群发4条,有点郁闷哦!),群发对象可选择为全部用户或分组用户,和由于节省群发次数,这里我就不测试群发文字信息了,具体参考如下代码:

绑定本月剩余群发条数

 ///  
 /// 绑定本月剩余群发条数
 /// 
 private void BindMassCount()
 {
 WxMassService wms = new WxMassService();
 List wxmaslist = wms.GetMonthMassCount();
 //官方微信服务号每月只能群发4条信息,(订阅号每天1条)多余信息,将不会成功推送,这里已经设定为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('群发信息已达上限!请下月初再试!')");
 }
 else
 {
 this.LinkBtnSubSend.Enabled = true;
 this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('您确定要群发此条信息??')");
 }
 }

绑定分组列表

 /// 
 /// 绑定分组列表
 /// 
 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(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();

 //设置缓存的数据7000秒后过期
 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 
 /// 选择群发对象类型,显示隐藏分组列表项
 /// 
 /// 
 /// 
 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;
 }
 }

群发

 /// 
 /// 群发
 /// 
 /// 
 /// 
 protected void LinkBtnSubSend_Click(object sender, EventArgs e)
 {
 //根据单选按钮判断类型,发送
 ///如果选择的是文本消息
 if (this.RadioBtnList.SelectedValue.ToString().Equals("0"))
 {
 if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入您要群发文本内容!');", true);
  return;
 }
 if (this.txtwenben.InnerText.ToString().Trim().Length<10)
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('文本内容至少需要10个字符以上!');", true);
  return;
 }

 WxMassService wms = new WxMassService();
 List wxmaslist = wms.GetMonthMassCount();

 if (wxmaslist.Count >= 4)
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群发消息数量已达上限!');", true);
  return;
 }
 else
 {


  //如何群发类型为全部用户,根据openID列表群发给全部用户,订阅号不可用,服务号认证后可用
  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();

  //设置缓存的数据7000秒后过期
  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&#63;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("0"))
  {
   //群发成功后,保存记录
  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 = "成功";//群发成功之后返回的状态码
  wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息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('群发任务已提交成功!!!数据已保存!');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(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&#63;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("0"))
  {
  //群发成功后,保存记录
  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 = "成功";//群发成功之后返回的状态码
  wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息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('群发任务已提交成功!!!数据已保存!');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("1"))
 {
 if (String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请选择或新建图文素材再进行群发!');", true);
  return;
 }

 WxMassService wms = new WxMassService();

 List wxmaslist = wms.GetMonthMassCount();

 if (wxmaslist.Count >= 4)
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群发消息数量已达上限!');", true);
  return;
 }
 else
 {
  
  //如何群发类型为全部用户,根据openID列表群发给全部用户,订阅号不可用,服务号认证后可用
  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();

  //设置缓存的数据7000秒后过期
  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&#63;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("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 = "成功";//群发成功之后返回的状态码
  wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息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('群发任务已提交成功!!!数据已保存!');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("0"))
  {
   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 > 0)
  {
   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(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&#63;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("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 = "成功";//群发成功之后返回的状态码
  wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息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('群发任务已提交成功!!!数据已保存!');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("0"))
  {
   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 > 0)
  {
   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;
  }
  }
 }
 }
 }

发送前预览

/// 
 /// 发送前预览
 /// 
 /// 
 /// 
 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(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&#63;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('请输入您要发送预览的文本内容!');", 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("0"))
 {
  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("1"))
 {
 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("0"))
 {
  Session["media_id"] = null;
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true);
  return;
 }


 }
 
 }

关键部分,获取全部用户的openID并串联成字符串:

 /// 
 /// 获取所有微信用户的OpenID
 /// 
 /// 
 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(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&#63;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(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 

本文已被整理到了《ASP.NET微信开发教程汇总》,欢迎大家学习阅读。

至此结束,下一章将继续讲解群发图文信息,因群发图文信息之前,需要先上传图文信息所需的素材,获取media_id,所以本章不做介绍,下一章将介绍新建单图文信息并群发,希望大家喜欢。


推荐阅读
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • RouterOS 5.16软路由安装图解教程
    本文介绍了如何安装RouterOS 5.16软路由系统,包括系统要求、安装步骤和登录方式。同时提供了详细的图解教程,方便读者进行操作。 ... [详细]
  • 小程序自动授权和手动接入的方式及操作步骤
    本文介绍了小程序支持的两种接入方式:自动授权和手动接入,并详细说明了它们的操作步骤。同时还介绍了如何在两种方式之间切换,以及手动接入后如何下载代码包和提交审核。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • GreenDAO快速入门
    前言之前在自己做项目的时候,用到了GreenDAO数据库,其实对于数据库辅助工具库从OrmLite,到litePal再到GreenDAO,总是在不停的切换,但是没有真正去了解他们的 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • Asp.net Mvc Framework 七 (Filter及其执行顺序) 的应用示例
    本文介绍了在Asp.net Mvc中应用Filter功能进行登录判断、用户权限控制、输出缓存、防盗链、防蜘蛛、本地化设置等操作的示例,并解释了Filter的执行顺序。通过示例代码,详细说明了如何使用Filter来实现这些功能。 ... [详细]
  • 深入解析Linux下的I/O多路转接epoll技术
    本文深入解析了Linux下的I/O多路转接epoll技术,介绍了select和poll函数的问题,以及epoll函数的设计和优点。同时讲解了epoll函数的使用方法,包括epoll_create和epoll_ctl两个系统调用。 ... [详细]
  • LVS实现负载均衡的原理LVS负载均衡负载均衡集群是LoadBalance集群。是一种将网络上的访问流量分布于各个节点,以降低服务器压力,更好的向客户端 ... [详细]
  • 本文介绍了NetCore WebAPI开发的探索过程,包括新建项目、运行接口获取数据、跨平台部署等。同时还提供了客户端访问代码示例,包括Post函数、服务器post地址、api参数等。详细讲解了部署模式选择、框架依赖和独立部署的区别,以及在Windows和Linux平台上的部署方法。 ... [详细]
author-avatar
股民连长
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有