C# wx獲取token的基本方法
更新時(shí)間:2017年06月19日 09:01:42 作者:猿大頭
這篇文章主要為大家詳細(xì)介紹了C# wx獲取token的基本方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C# wx獲取token的具體代碼,供大家參考,具體內(nèi)容如下
#region 請(qǐng)求Url,不發(fā)送數(shù)據(jù) /// <summary> /// 請(qǐng)求Url,不發(fā)送數(shù)據(jù) /// </summary> public static string RequestUrl(string url) { return RequestUrl(url, "POST"); } #endregion #region 請(qǐng)求Url,不發(fā)送數(shù)據(jù) /// <summary> /// 請(qǐng)求Url,不發(fā)送數(shù)據(jù) /// </summary> public static string RequestUrl(string url, string method) { // 設(shè)置參數(shù) var request = WebRequest.Create(url) as HttpWebRequest; var cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = method; request.ContentType = "text/html"; request.Headers.Add("charset", "utf-8"); //發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù) var response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才開(kāi)始向目標(biāo)網(wǎng)頁(yè)發(fā)送Post請(qǐng)求 Stream responseStream = response.GetResponseStream(); var sr = new StreamReader(responseStream, Encoding.UTF8); //返回結(jié)果網(wǎng)頁(yè)(html)代碼 string content = sr.ReadToEnd(); return content; } #endregion #region 獲取Json字符串某節(jié)點(diǎn)的值 /// <summary> /// 獲取Json字符串某節(jié)點(diǎn)的值 /// </summary> public static string GetJsonValue(string jsonStr, string key) { string result = string.Empty; if (!string.IsNullOrEmpty(jsonStr)) { key = "\"" + key.Trim('"') + "\""; int index = jsonStr.IndexOf(key) + key.Length + 1; if (index > key.Length + 1) { //先截逗號(hào),若是最后一個(gè),截"}"號(hào),取最小值 int end = jsonStr.IndexOf(',', index); if (end == -1) { end = jsonStr.IndexOf('}', index); } result = jsonStr.Substring(index, end - index); result = result.Trim(new [] {'"', ' ', '\"'}); //過(guò)濾引號(hào)或空格 } } return result; } #endregion #region 驗(yàn)證Token是否過(guò)期 /// <summary> /// 驗(yàn)證Token是否過(guò)期 /// </summary> public static bool TokenExpired(string access_token) { string jsonStr = RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", access_token)); if (GetJsonValue(jsonStr, "errcode") == "42001") { return true; } return false; } #endregion #region 獲取Token /// <summary> /// 獲取Token /// </summary> public static string GetToken(string appid, string secret) { string strJson = RequestUrl( string.Format( "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret)); return GetJsonValue(strJson, "access_token"); } #endregion //獲取Openid public static string GetOpenId(string appid, string secret, string code) { string strJson = RequestUrl( string.Format( "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code", appid, secret, code)); //LogUtil.WriteLog(strJson); return GetJsonValue(strJson, "openid"); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
DataGridView帶圖標(biāo)的單元格實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了DataGridView帶圖標(biāo)的單元格的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08C#實(shí)現(xiàn)Winform版計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)Winform版計(jì)算器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET
這篇文章主要用asp.net技術(shù)實(shí)現(xiàn)直接在線預(yù)覽word、excel、txt文件,有需要的朋友可以參考下2015-08-08C#中System.Text.Json匿名對(duì)象反序列化
這篇文章主要介紹了System.Text.Json匿名對(duì)象反序列化,下文代碼基于. NET 6,為了代碼整潔,實(shí)際配置了PropertyNameCaseInsensitive = true,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05C#結(jié)合JavaScript實(shí)現(xiàn)手寫(xiě)板簽名效果
這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合JavaScript實(shí)現(xiàn)手寫(xiě)板寫(xiě)字并上傳到服務(wù)器進(jìn)行處理,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04C#中IEnumerable、ICollection、IList、List之間的區(qū)別
IEnumerable、ICollection、IList、List之間的區(qū)別,本文分別分析了它的實(shí)現(xiàn)源碼,從而總結(jié)出了它們之間的關(guān)系和不同之處。對(duì)C# IEnumerable、ICollection、IList、List相關(guān)知識(shí),感興趣的朋友一起看看吧2021-07-07