C#獲取微信公眾號的access_token


        static public string access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        static public string GetAccessToken(string appid, string appsecret)
        {
            string requestUrl = access_token_url.Replace("APPID", appid).Replace("APPSECRET", appsecret);
            WebResponse result = null;
            WebRequest req = WebRequest.Create(requestUrl);
            result = req.GetResponse();
            Stream s = result.GetResponseStream();
            XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(s, XmlDictionaryReaderQuotas.Max);
            xmlReader.Read();
            string xml = xmlReader.ReadOuterXml();
            s.Close();
            s.Dispose();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlElement rootElement = doc.DocumentElement;
            string access_token = rootElement.SelectSingleNode("access_token").InnerText.Trim();
            return access_token;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM