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

asp.net微信开发中有关高级群发文本的示例分析

小编给大家分享一下asp.net微信开发中有关高级群发文本的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读

小编给大家分享一下asp.net微信开发中有关高级群发文本的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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

asp.net微信开发中有关高级群发文本的示例分析

asp.net微信开发中有关高级群发文本的示例分析

看图我们也可以看出首先我们要获取该微信号本月还能群发几条信息,关于怎么计算,就是群发成功一条信息,就在本地数据库存储一条信息,用来计算条数,(这个我相信都会),大于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 < groupsnum; i++)
 {
 this.DDLGroupList.Items.Add(new ListItem(jsonObj["groups"][i]["name"].ToString() + "(" + jsonObj["groups"][i]["count"].ToString() + ")", jsonObj["groups"][i]["id"].ToString()));
 }
 }
 /// 
 /// 选择群发对象类型,显示隐藏分组列表项
 /// 
 /// 
 /// 
 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(&#39;请输入您要群发文本内容!&#39;);", true);
  return;
 }
 if (this.txtwenben.InnerText.ToString().Trim().Length<10)
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;文本内容至少需要10个字符以上!&#39;);", true);
  return;
 }

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

 if (wxmaslist.Count >= 4)
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;本月可群发消息数量已达上限!&#39;);", 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?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(&#39;群发任务已提交成功!!!数据已保存!&#39;);location=&#39;WxMassManage.aspx&#39;;", true);
  return;
  }
  else
  {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务已提交成功!!!数据保存失败!&#39;);", true);
  return;
  }
  }
  else
  {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务提交失败!!&#39;);", 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?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(&#39;群发任务已提交成功!!!数据已保存!&#39;);location=&#39;WxMassManage.aspx&#39;;", true);
  return;
  }
  else
  {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务已提交成功!!!数据保存失败!&#39;);", true);
  return;
  }
  }
  else
  {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务提交失败!!&#39;);", 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(&#39;请选择或新建图文素材再进行群发!&#39;);", true);
  return;
 }

 WxMassService wms = new WxMassService();

 List wxmaslist = wms.GetMonthMassCount();

 if (wxmaslist.Count >= 4)
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;本月可群发消息数量已达上限!&#39;);", 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?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(&#39;群发任务已提交成功!!!数据已保存!&#39;);location=&#39;WxMassManage.aspx&#39;;", true);
   return;
  }
  else
  {
   ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务已提交成功!!!数据保存失败!&#39;);", 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(&#39;群发任务已提交成功!!!图文部分数据已保存!&#39;);location=&#39;WxMassManage.aspx&#39;;", true);
   return;
  }
  else
  {
   ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务已提交成功!!!数据保存失败!&#39;);", true);
   return;
  }
  }
  }
  else
  {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务提交失败!!&#39;);", 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?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(&#39;群发任务已提交成功!!!数据已保存!&#39;);location=&#39;WxMassManage.aspx&#39;;", true);
   return;
  }
  else
  {
   ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务已提交成功!!!数据保存失败!&#39;);", 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(&#39;群发任务已提交成功!!!图文部分数据已保存!&#39;);location=&#39;WxMassManage.aspx&#39;;", true);
   return;
  }
  else
  {
   ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务已提交成功!!!数据保存失败!&#39;);", true);
   return;
  }
  }
  }
  else
  {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;群发任务提交失败!!&#39;);", 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?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(&#39;请输入您要发送预览的文本内容!&#39;);", true);
  return;
 }
 if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号"))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;请输入接收消息的用户微信号!&#39;);", 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(&#39;发送预览成功!!&#39;);", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;发送预览失败!!&#39;);", 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(&#39;请选择要预览的图文素材!&#39;);", true);
  return;
 }
 if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号"))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;请输入接收消息的用户微信号!&#39;);", 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(&#39;发送预览成功!!&#39;);", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;发送预览失败!!&#39;);", 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?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 < totalnum; i++)
 {
  sb.Append(&#39;"&#39;);
  sb.Append(jsonObj["data"]["openid"][i].ToString());
  sb.Append(&#39;"&#39;);
  sb.Append(",");
 }
 }

 return sb.Remove(sb.ToString().LastIndexOf(","),1).ToString();
 }

以上是“asp.net微信开发中有关高级群发文本的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程笔记行业资讯频道!


推荐阅读
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 数字账号安全与数据资产问题的研究及解决方案
    本文研究了数字账号安全与数据资产问题,并提出了解决方案。近期,大量QQ账号被盗事件引起了广泛关注。欺诈者对数字账号的价值认识超过了账号主人,因此他们不断攻击和盗用账号。然而,平台和账号主人对账号安全问题的态度不正确,只有用户自身意识到问题的严重性并采取行动,才能推动平台优先解决这些问题。本文旨在提醒用户关注账号安全,并呼吁平台承担起更多的责任。令牌云团队对此进行了长期深入的研究,并提出了相应的解决方案。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 在Java中,我会做这样的事情:classPerson{privateRecordrecord;publicStringname(){record().get(name);}p ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
author-avatar
徐新nina
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有