/// <summary> /// 根据当前日期 判断Access_Token 是否超期 如果超期返回新的Access_Token 否则返回之前的Access_Token /// </summary> /// <param name="datetime"></param> /// <returns></returns> public static string IsExistAccess_Token() { string Token = string.Empty; DateTime YouXRQ; // 读取XML文件中的数据,并显示出来 ,注意文件路径 string filepath = System.Web.HttpContext.Current.Server.MapPath("/XMLWXACCESS.xml"); StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8); XmlDocument xml = new XmlDocument(); xml.Load(str); str.Close(); str.Dispose(); Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText; YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText); if (DateTime.Now > YouXRQ) { DateTime _youxrq = DateTime.Now; Access_token mode = GetAccess_token(); if (mode.access_token != null && mode.expires_in != null) { xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token; _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in)); xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString(); xml.Save(filepath); Token = mode.access_token; } } return(Token); //string _access_token = IsExistAccess_Token(); }
/// <summary> /// 获取微信Token /// </summary> /// <returns></returns> //public static string GetToken() //{ // try // { // string getAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; // string respText = ""; // //获取appid和appsercret // string wechat_appid = "wx14b829fae5b27750"; // string wechat_appsecret = "b316ef7d7c1c4bd9dad8141e252f6da7"; // //获取josn数据 // string url = string.Format(getAccessTokenUrl, wechat_appid, wechat_appsecret); // string key = WebKeys.AccessTokenCacheKey; // AccessToken access = MyCache<AccessToken>.Get(key); // if (access == null) // { // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // using (Stream resStream = response.GetResponseStream()) // { // StreamReader reader = new StreamReader(resStream, Encoding.Default); // respText = reader.ReadToEnd(); // resStream.Close(); // } // JavaScriptSerializer Jss = new JavaScriptSerializer(); // Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText); // //通过键access_token获取值 // access = new AccessToken(); // access.accessToken = respDic["access_token"].ToString(); // access.lastGetTicketTime = DateTime.Now; // MyCache<AccessToken>.Insert(key, access, 1400); // } // return access.accessToken; // #region // ////ticket 缓存7200秒 // //if (System.Web.HttpContext.Current.Session["jsapi_ticket"] == null) // //{ // // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // // HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // // using (Stream resStream = response.GetResponseStream()) // // { // // StreamReader reader = new StreamReader(resStream, Encoding.Default); // // respText = reader.ReadToEnd(); // // resStream.Close(); // // } // // JavaScriptSerializer Jss = new JavaScriptSerializer(); // // Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText); // // //通过键access_token获取值 // // accessToken = respDic["access_token"].ToString(); // // System.Web.HttpContext.Current.Session["jsapi_ticket"] = accessToken; // // System.Web.HttpContext.Current.Session.Timeout = 7200; // // return accessToken; // //} // //else // //{ // // accessToken = System.Web.HttpContext.Current.Session["jsapi_ticket"].ToString(); // // return accessToken; // //} // #endregion // } // catch (Exception ex) // { // LogUtil.WriteLog(ex); // return null; // } //} public static Access_token GetAccess_token() { string appid = System.Configuration.ConfigurationManager.AppSettings["weixinappid"]; string secret = System.Configuration.ConfigurationManager.AppSettings["weixincode"]; string strUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; Access_token mode = new Access_token(); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl); req.Method = "GET"; using (WebResponse wr = req.GetResponse()) { HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); string content = reader.ReadToEnd(); //Response.Write(content); //在这里对Access_token 赋值 Access_token token = new Access_token(); token = JsonHelper.ParseFromJson <Access_token>(content); mode.access_token = token.access_token; mode.expires_in = token.expires_in; } return(mode); }