c#保存token,判断时间过了几小时重新获取

1.//定义一个全局
 public static string access_token="";

2.//判断保存时间 和当前时间过了几小时
  //当前时间
                    DateTime beginTime1 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
//保存时间
jotime = (JObject)JsonConvert.DeserializeObject(access_token);
DateTime endTime1 = DateTime.Parse(jotime["time"].ToString());
TimeSpan midTime =  beginTime1- endTime1;
 //超过5小时就替换
 if (midTime.Hours>=1)
 {
    gettoken();
 }
3.//获取token
    public void gettoken()
        {
        
var str = http.GetSend("https://oauth.open.weidian.com/token?grant_type=client_credential&appkey=*&secret=*", "", null, Encoding.UTF8, true);

       JObject jo = (JObject)JsonConvert.DeserializeObject(str);
     
       //保存token
       access_token = "{\"token\":\"" + jo["result"]["access_token"].ToString() + "\",\"time\":\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\"}";
            jotime = (JObject)JsonConvert.DeserializeObject(access_token);
        }

 

你可能感兴趣的:(.NET,c#,.NET,TOKEN)