예제 #1
0
        /// <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("/XMLFile.xml");
            string errorfilepath = System.Web.HttpContext.Current.Server.MapPath("/Error.xml");

            StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
            XmlDocument  xml = new XmlDocument();

            xml.Load(str);
            str.Close();
            str.Dispose();

            StreamReader errorstr = new StreamReader(errorfilepath, System.Text.Encoding.UTF8);
            XmlDocument  errorXml = new XmlDocument();

            errorXml.Load(errorstr);
            errorstr.Close();
            errorstr.Dispose();

            Token  = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
            YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);

            errorXml.SelectSingleNode("error").SelectSingleNode("errorstr").InnerText = "date later(" + DateTime.Now.AddMinutes(-2) + ") than now(" + YouXRQ + ")";
            if (DateTime.Now.AddMinutes(-2) > YouXRQ)
            {
                DateTime     _youxrq = DateTime.Now;
                Access_token mode    = GetAccess_token();

                //XmlElement node = errorXml.CreateElement("access_token");
                //node.InnerText = mode.access_token;
                //errorXml.SelectSingleNode("error").AppendChild(node);

                if (!string.IsNullOrEmpty(mode.access_token))
                {
                    xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
                    _youxrq = _youxrq.AddSeconds(mode.expires_in);
                    xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
                    xml.Save(filepath);
                    Token = mode.access_token;
                }
            }
            errorXml.Save(errorfilepath);
            return(Token);
        }
예제 #2
0
        public static Access_token GetAccess_token()
        {
            Inition();
            string       url    = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + WxPayConfig.APPSECRET;
            string       result = HttpService.Get(url);
            Access_token mode   = new Access_token();

            if (!string.IsNullOrEmpty(result))
            {
                JObject jd = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result);

                if (jd.Property("access_token") != null)
                {
                    mode.access_token = (string)jd["access_token"];
                    mode.expires_in   = (int)jd["expires_in"];
                }
                else
                {
                    Log.Debug("GET ACCESS_TOKEN:", result + " url:" + url);
                }
            }
            return(mode);
        }