博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【微信开发】—7200s限制,非数据库
阅读量:6385 次
发布时间:2019-06-23

本文共 3426 字,大约阅读时间需要 11 分钟。

两种微信缓存方式(7200s)

第一种是MemoryCache(缓存的分享票据)

public static string Getjsapi_ticket(string AppID, string AppSecret)        {            string tt = "";            string ticket = AddOrGetExisting
("ticket", delegate() { string strUrl = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppID, AppSecret); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl); WebResponse response = request.GetResponse(); Stream resStream = response.GetResponseStream(); StreamReader sr = new StreamReader(resStream); string result = sr.ReadToEnd(); string regex = "\"access_token\":\"(?
.*?)\""; Match mt = Regex.Match(result, regex); string token = ""; if (mt.Success) { token = mt.Groups["token"].Value; } string jsapi_url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", token); HttpWebRequest jsapi_request = (HttpWebRequest)WebRequest.Create(jsapi_url); WebResponse jsapi_response = jsapi_request.GetResponse(); Stream jsapi_resStream = jsapi_response.GetResponseStream(); StreamReader jsapi_sr = new StreamReader(jsapi_resStream); string jsapi_result = jsapi_sr.ReadToEnd(); string temp = jsapi_result.Split(',')[2]; string jsapi_tic = temp.Split(':')[1]; jsapi_tic = jsapi_tic.Substring(1, jsapi_tic.Length - 2); return jsapi_tic; }, new TimeSpan(0, 0, 7000)//7000秒过期 ); return ticket; } static readonly ObjectCache cache = MemoryCache.Default; public static T AddOrGetExisting
(string key, Func
createNew, TimeSpan cacheDuration) { return AddOrGetExisting
(key, new TimeSpan(0, 0, 7000), createNew); } public static T AddOrGetExisting
(string key, TimeSpan cacheDuration, Func
createNew) { if (key == null) throw new ArgumentNullException("key"); if (createNew == null) throw new ArgumentNullException("createNew"); if (!cache.Contains(key)) { cache.Add(key, createNew(), DateTime.Now.Add(cacheDuration)); } return (T)cache[key]; }

  第二种HttpContext.Current.Cache(缓存的access_token)

/// /// 获取公众号的ACCESS_TOKEN/// /// 
返回操作凭据
public string GetAccessToken(){  if (HttpContext.Current.Cache["access_token"] == null) {   string para = string.Format("grant_type=client_credential&appid={0}&secret={1}", AppID, AppSecret);   string results = SendHTTPRequest("POST", "https://api.weixin.qq.com/cgi-bin/token", para);   JObject obj = (JObject)JsonConvert.DeserializeObject(results);   //设置access_token的过期   Cache cache = HttpContext.Current.Cache;   cache.Insert("access_token", obj["access_token"].ToString(), null, DateTime.Now.AddSeconds(7000),      System.Web.Caching.Cache.NoSlidingExpiration);   return HttpContext.Current.Cache["access_token"].ToString(); } else {   return HttpContext.Current.Cache["access_token"].ToString(); }}

  

转载于:https://www.cnblogs.com/lb12081116/p/5226721.html

你可能感兴趣的文章
一只年轻而倒悬的梨
查看>>
解决time_wait过多的问题
查看>>
技术转载:Jni学习一:了解Jni
查看>>
vue教程2-07 自定义指令
查看>>
Node.js之循环依赖
查看>>
python3调用阿里云短信服务
查看>>
Linux-百度云之AccleriderMini使用
查看>>
bootstrapTable refresh 方法使用简单举例
查看>>
2、TestNG+Maven+IDEA环境搭建
查看>>
maven插件运行过程中自动执行sql文件
查看>>
New UWP Community Toolkit - XAML Brushes
查看>>
C# ==、Equals、ReferenceEquals 区别与联系 (转载)
查看>>
layer弹出层的关闭问题
查看>>
LeetCode——3Sum & 3Sum Closest
查看>>
netstat详解
查看>>
微信小程序 --- e.currentTarget.dataset.id 获取不到值
查看>>
Introducing stapbpf – SystemTap’s new BPF backend
查看>>
详细介绍MySQL/MariaDB的锁
查看>>
0603-Zuul构建API Gateway-通过Zuul上传文件,禁用Zuul的Filter
查看>>
cocos2dx-2.x CCFileUtils文件管理分析(2)
查看>>