Ejemplo n.º 1
0
        public static Access_token GetAccessTokenFromWX()
        {
            string       appid  = System.Configuration.ConfigurationManager.AppSettings["appid"].ToString();  //; "wx072d7c1098af8577";
            string       secret = System.Configuration.ConfigurationManager.AppSettings["secret"].ToString(); //; "67cc96a576ea08739dfef2c5a18d4172";
            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             = JSBase.JsonHelper.ParseFromJson <Access_token>(content);
                mode.access_token = token.access_token;
                mode.expires_in   = token.expires_in;
            }
            return(mode);
        }
Ejemplo n.º 2
0
        public string GetAccessToken()
        {
            string   Token = string.Empty;
            DateTime YouXRQ;
            // 读取XML文件中的数据,并显示出来 ,注意文件路径
            string filepath;// = Server.MapPath("XMLFile.xml");

            filepath = System.Web.Hosting.HostingEnvironment.MapPath("/");
            //if (MyServer == null)
            //    filepath = Server.MapPath("/");
            //else
            //    filepath = MyServer.MapPath("/");
            filepath = filepath + "\\wechat\\XMLFile.xml";

            if (System.IO.File.Exists(filepath))
            {
                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    = GetAccessTokenFromWX();
                    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;
                }
            }
            else
            {
                if (!System.IO.Directory.Exists(filepath.Substring(0, filepath.LastIndexOf("\\"))))
                {
                    System.IO.Directory.CreateDirectory(filepath.Substring(0, filepath.LastIndexOf("\\")));
                }
                XmlDocument  xml     = new XmlDocument();
                DateTime     _youxrq = DateTime.Now;
                Access_token mode    = GetAccessTokenFromWX();

                XmlElement x = xml.CreateElement("xml");
                xml.AppendChild(x);

                XmlElement Access_Token = xml.CreateElement("Access_Token");
                Access_Token.InnerText = mode.access_token;
                x.AppendChild(Access_Token);
                XmlElement Access_YouXRQ = xml.CreateElement("Access_YouXRQ");
                _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in));
                Access_YouXRQ.InnerText = _youxrq.ToString();
                x.AppendChild(Access_YouXRQ);

                //xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
                //xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
                xml.Save(filepath);
                Token = mode.access_token;
            }
            return(Token);
        }