public static string GetJsApiTicket() { string[] res; string jstoken = ""; try { //string confilgFile = GetConfigFilePath("access_jsapi_token.txt"); //string result = File.ReadAllText(confilgFile); //res = result.Split('&'); //TimeSpan time = (DateTime.Now - DateTime.Parse(res[1])); //if (time.Hours >= 2 || time.Days > 0)//正常情况下AccessToken有效期为7200秒,有效期内重复获取返回相同结果,并自动续期。 //{ // string strs = GetJsApiTicketNoCache(); // return strs.Substring(0, strs.LastIndexOf('&')); //} //else // return res[0]; string token = GetTokenUtils.GetToken(); string strUrl = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token + "&type=jsapi"); string returndate = HttpClient.HttpGet(strUrl, ""); string stoken = returndate.Split(',')[2].Split(':')[1].ToString(); jstoken = stoken.Replace("\"", ""); } catch (Exception) { string strs = GetJsApiTicketNoCache(); return(strs.Substring(0, strs.LastIndexOf('&'))); } return(jstoken); }
public static String GetJsApiTicketNoCache() { string token = GetTokenUtils.GetToken(); //string strUrl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + token); string strUrl = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token + "&type=jsapi"); string returndate = HttpClient.HttpGet(strUrl, ""); string stoken = returndate.Split(',')[2].Split(':')[1].ToString(); string jstoken = stoken.Replace("\"", ""); string result = jstoken + "&" + DateTime.Now; //XDJHKSHHFJFFHFH: TIME string configFile = GetConfigFilePath("access_jsapi_token.txt"); //避免重复请求令牌,应进行全局静态存储 File.WriteAllText(configFile, result); return(result); }