//记录access token 申请时的时间
private static DateTime GetAccessToken_Time;
/// <summary>
/// 过期时间为7200秒
/// </summary>
private static int Expires_Period = ;
private static string mAccessToken;
/// <summary>
/// 获取access token
/// </summary>
/// <returns></returns>
private static string GetAccessToken()
{
if (string.IsNullOrEmpty(mAccessToken) || HasExpired())
{
string Url = "https://api.weixin.qq.com/cgi-bin/token";
string RequestPara = "grant_type=client_credential&appid=" + APPID + "&secret=" + APPSECRET;
string result = WebRequestHelper.GetData(RequestPara, Url);
if (!result.Equals(""))
{
GetAccessToken_Time = DateTime.Now;
accesstoken token = JsonFormat.Deserialize<accesstoken>(result);
Expires_Period = Convert.ToInt32(token.expires_in);
return token.access_token;
}
else
{
GetAccessToken_Time = DateTime.MinValue;
}
}
return null;
}
/// <summary>
/// 判断Access_token是否过期
/// </summary>
/// <returns>bool</returns>
private static bool HasExpired()
{
if (GetAccessToken_Time != null)
{
//过期时间,允许有一定的误差,一分钟。获取时间消耗
if (DateTime.Now > GetAccessToken_Time.AddSeconds(Expires_Period).AddSeconds(-))
{
return true;
}
}
return false;
}