三、令牌优化.
前面我们已经知道了,怎么获取我们的access_token,我们知道它的有效时间是7200s,在这里难道我们每次请求都需要去重新获取一次,岂不是很麻烦,而且也不利于性能的优化,在这里我们对这个获取令牌的方法做一些优化,更有利于我们的代码.
/// summary>
/// 微信许可令牌
/// /summary>
public class AccessToken
{
/// summary>
/// 保存已获取到的许可令牌,键为公众号,值为公众号最后一次获取到的令牌
/// /summary>
private static ConcurrentDictionarystring, TupleAccessToken, DateTime>> accessTokens = new ConcurrentDictionarystring, TupleAccessToken, DateTime>>();
/// summary>
/// 获取access token的地址
/// /summary>
private const string urlForGettingAccessToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credentialappid={0}secret={1}";
/// summary>
/// 获取access token的http方法
/// /summary>
private const string httpMethodForGettingAccessToken = WebRequestMethods.Http.Get;
/// summary>
/// 保存access token的最长时间(单位:秒),超过时间之后,需要重新获取
/// /summary>
private const int accessTokenSavingSeconds = 7000;
/// summary>
/// access token
/// /summary>
public string access_token { get; set; }
/// summary>
/// 有效时间,单位:秒
/// /summary>
public int expires_in { get; set; }
/// summary>
/// 构造函数
/// /summary>
/// param name="_access_token">access token/param>
/// param name="_expires_in">有效时间/param>
internal AccessToken(string _access_token, int _expires_in)
{
access_token = _access_token;
expires_in = _expires_in;
}
/// summary>
/// 返回AccessToken字符串
/// /summary>
/// returns>/returns>
public override string ToString()
{
return string.Format("AccessToken:{0}\r\n有效时间:{1}秒", access_token, expires_in);
}
/// summary>
/// 从JSON字符串解析AccessToken
/// /summary>
/// param name="json">JSON字符串/param>
/// returns>返回AccessToken/returns>
internal static AccessToken ParseFromJson(string json)
{
var at = JsonConvert.DeserializeAnonymousType(json, new { access_token = "", expires_in = 1 });
return new AccessToken(at.access_token, at.expires_in);
}
/// summary>
/// 尝试从JSON字符串解析AccessToken
/// /summary>
/// param name="json">JSON字符串/param>
/// param name="msg">如果解析成功,返回AccessToken;否则,返回null。/param>
/// returns>返回是否解析成功/returns>
internal static bool TryParseFromJson(string json, out AccessToken token)
{
bool success = false;
token = null;
try
{
token = ParseFromJson(json);
success = true;
}
catch { }
return success;
}
/// summary>
/// 得到access token
/// /summary>
/// param name="userName">公众号/param>
/// returns>返回access token/returns>
public static AccessToken Get(string userName)
{
TupleAccessToken, DateTime> lastToken = accessTokens.ContainsKey(userName) ? accessTokens[userName] : null;
AccessToken token = lastToken == null ? null : lastToken.Item1;
DateTime refreshTime = lastToken == null ? DateTime.MinValue : lastToken.Item2;
double ms = (DateTime.Now - refreshTime).TotalSeconds;
if (ms > accessTokenSavingSeconds || token == null)
{
//尝试从微信服务器获取2次
ErrorMessage msg;
AccessToken newToken = GetFromWeixinServer(userName, out msg);
if (newToken == null)
newToken = GetFromWeixinServer(userName, out msg);
if (newToken != null)
{
lastToken = new TupleAccessToken, DateTime>(newToken, DateTime.Now);
accessTokens[userName] = lastToken;
token = newToken;
}
}
return token;
}
/// summary>
/// 从微信服务器获取access token
/// /summary>
/// param name="userName">公众号/param>
/// param name="msg">从服务器返回的错误信息。/param>
/// returns>返回许可令牌;如果获取失败,返回null。/returns>
private static AccessToken GetFromWeixinServer(string userName, out ErrorMessage msg)
{
AccessToken token = null;
msg = new ErrorMessage(ErrorMessage.ExceptionCode, "");
string url = string.Format(urlForGettingAccessToken, WxPayConfig.APPID, WxPayConfig.APPSECRET);
string result;
if(!HttpHelper.Request(url, out result, httpMethodForGettingAccessToken, string.Empty))
{
msg.errmsg = "从微信服务器获取响应失败。";
return token;
}
if (ErrorMessage.IsErrorMessage(result))
msg = ErrorMessage.Parse(result);
else
{
try
{
token = AccessToken.ParseFromJson(result);
}
catch (Exception e)
{
msg = new ErrorMessage(e);
}
}
return token;
}
}
从这个类中的代码我们看到,每次获取access_token的时候都会判断一些时间是不是超过了7000,我们的token过期时间是7200s,这样就不需要每次请求的时候都是重新获取token。
这个时候我们的查询代码可以优化下.
protected void Page_Load(object sender, EventArgs e)
{
string username = System.Configuration.ConfigurationManager.AppSettings["weixinid"].ToString();
AccessToken token = AccessToken.Get(username);
GetPage("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + access_token + "");
//GetPage("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=access_token");
}