Exemplo n.º 1
0
        /// <summary>
        /// 获取Access_token值
        /// </summary>
        /// <returns></returns>
        public static string IsExistAccess_Token(string corpid, string secret)
        {
            string   Token = string.Empty;
            DateTime YouXRQ;
            // 读取XML文件中的数据,并显示出来 ,注意文件路径
            string       filepath = Directory.GetCurrentDirectory() + "/XMLFile1.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(corpid, secret);
                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);
        }
Exemplo n.º 2
0
        private static Access_token GetAccess_token(string corpid, string secret)
        {
            string         strUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + 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();//在这里对Access_token 赋值
                Access_token    token      = new Access_token();
                token             = Newtonsoft.Json.JsonConvert.DeserializeObject <Access_token>(content);
                mode.access_token = token.access_token;
                mode.expires_in   = token.expires_in;
            }
            return(mode);
        }