Esempio n. 1
0
 public static T Post(string url, string param = null)
 {
     if (url != "")
     {
         try
         {
             HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Post(url, param, "application/json");
             T model = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(result.ToStringResult());
             return(model);
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
     else
     {
         return(default(T));
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 获取/更新票据
        /// </summary>
        /// <param name="forced">true:强制更新.false:按缓存是否到期来更新</param>
        /// <returns></returns>
        public static AccessToken GetAccessToken(bool forced = false)
        {
            AccessToken model = AccessToken;

            //TokenCacheTime是缓存时间(常量,也可放到配置文件中),这样在有效期内则直接从缓存中获取票据,不需要再向服务器中获取。
            if (!forced && AccessToken.Begin.AddSeconds(DingTalkUrlHelp.TokenCacheTime) >= DateTime.Now)
            {
                //没有强制更新,并且没有超过缓存时间
                return(model);
            }

            string apiurl = DingTalkUrlHelp.GetTokenUrl;

            try
            {
                HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Get(apiurl);

                Get_AccessToken tokenResult = Newtonsoft.Json.JsonConvert.DeserializeObject <Get_AccessToken>(result.ToStringResult());

                if (tokenResult != null)
                {
                    if (tokenResult.errcode == 0)
                    {
                        AccessToken.Value = tokenResult.access_token;
                        AccessToken.Begin = DateTime.Now;
                        return(model);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper log = LogFactory.GetLogger("GetAccessToken");
                log.Error(ex);
                throw ex;
            }
            return(model);
        }