protected void Page_Load(object sender, EventArgs e) { if(Request.QueryString["id"]!=null) { String openid = Request.QueryString["id"].ToString(); this.txtOpenId.Value = openid.ToString(); //根据OpenID获取用户基本信息。缓存处理 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 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + Access_tokento + "&openid=" + openid; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres); myRequest.Method = "GET"; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); string content = reader.ReadToEnd(); reader.Close(); //使用前需要引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(content); //假如备注名不为空,给备注名文本框赋值,显示原有的备注名 if (!String.IsNullOrWhiteSpace(jsonObj["remark"].ToString())) { this.txtRemarkName.Value = jsonObj["remark"].ToString(); } } } /// <summary> /// 设置备注名 /// </summary> /// <param></param> /// <param></param> protected void LinkBtnSet_Click(object sender, EventArgs e) { String openid = Request.QueryString["id"].ToString(); WeiXinServer wxs = new WeiXinServer(); string res = ""; ///从缓存读取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/user/info/updateremark?access_token=" + Access_tokento; string postData = "{\"openid\":\"" + openid.ToString().Trim() + "\",\"remark\":\"" + this.txtRemarkName.Value.ToString() + "\"}"; res = wxs.GetPage(posturl, postData); //使用前需药引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(res); ///获取返回结果的正确|true|false, string isright = jsonObj["errcode"].ToString();//0 string istrueorfalse = jsonObj["errmsg"].ToString();//ok if (isright.Equals("0") && istrueorfalse.Equals("ok")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('修改备注成功!');location='https://www.jb51.net/WeiXinUserList.aspx';", true); } else { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('修改备注失败!');", true); } }
本文已被整理到了《ASP.NET微信开发教程汇总》,欢迎大家学习阅读。
以上就是已关注用户管理的全部核心代码,仅供参考,希望对大家的学习有所帮助。
您可能感兴趣的文章: