Esempio n. 1
0
 /// <summary>
 /// 根据当前日期 判断Access_Token 是否超期  如果超期返回新的Access_Token   否则返回之前的Access_Token
 /// </summary>
 /// <param name="datetime"></param>
 /// <returns></returns>
 public static string IsExistAccess_Token()
 {
     try
     {
         string   Token = string.Empty;
         DateTime YouXRQ;
         //读取XML文件中的数据,并显示出来
         string       filepath = HttpContext.Current.Request.MapPath("~/XMLFile.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();
             xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
             _youxrq = _youxrq.AddSeconds(Convert.ToInt32(mode.expires_in));
             xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
             xml.Save(filepath);
             Token = mode.access_token;
         }
         return(Token);
     }
     catch (Exception ex)
     {
         return("");//记录日志
     }
 }
Esempio n. 2
0
        public static Access_token GetAccess_Token()
        {
            string       url    = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + "wxcbe87bb4937385db" + "&secret=" + "1d380d569f1bb50f80b29d6ed21d4e4c";
            Access_token entity = new Access_token();

            try {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                req.Method = "GET";
                using (WebResponse wr = req.GetResponse()) {
                    HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                    StreamReader    reader     = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                    string          content    = reader.ReadToEnd();
                    Access_token    token      = new Access_token();
                    token = JsonHelper.ParseFromJson <Access_token>(content);
                    entity.access_token = token.access_token;
                    entity.expires_in   = token.expires_in;
                }
            } catch { //记录日志}
                return(entity);
            }
            return(entity);
        }