当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net微信开发(高级群发文本)

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

风驭无弹窗,北京市委书记李雪峰,姐夫拿麻将砸死小舅子

首先我们先来讲解一下群发文本信息的过程,我个人开发程序是首先要有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 = (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('您确定要群发此条信息??')");
 }
 }

绑定分组列表

 /// <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(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()));
 }
 }
 /// <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()) > 0)
 {
 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("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<wxmassinfo> 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?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?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<wxmassinfo> 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?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?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;
  }
  }
 }
 }
 }

发送前预览

/// <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(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('请输入您要发送预览的文本内容!');", 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并串联成字符串:

 /// <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(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('"');
  sb.append(jsonobj["data"]["openid"][i].tostring());
  sb.append('"');
  sb.append(",");
 }
 }

 return sb.remove(sb.tostring().lastindexof(","),1).tostring();
 }


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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网