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

/// <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微信开发教程汇总》,欢迎大家学习阅读。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwjfy.html