定时请求微信Access_Token
Posted Coder_小菜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定时请求微信Access_Token相关的知识,希望对你有一定的参考价值。
/// <summary> /// 设置缓存并在缓存超时两分钟前重新请求 /// </summary> private void SetAndAutoRefreshAccessToken() { object oToken = HttpContext.Current.Cache[WxConst.ACCESS_TOKEN]; string sToken = ""; int iExpiresIn = 0; if (oToken == null) { sToken = GetAccessToken(out iExpiresIn); // 缓存到期前更新缓存 HttpContext.Current.Cache.Insert(WxConst.ACCESS_TOKEN, sToken, null, DateTime.Now.AddSeconds(iExpiresIn), TimeSpan.Zero, (string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration) => { sToken = GetAccessToken(out iExpiresIn); // 设置缓存有效时间为微信access_token有效斯减120秒 iExpiresIn = iExpiresIn - 120; expensiveObject = sToken; dependency = null; absoluteExpiration = DateTime.Now.AddSeconds(iExpiresIn); slidingExpiration = TimeSpan.Zero; } ); } }
以上是关于定时请求微信Access_Token的主要内容,如果未能解决你的问题,请参考以下文章