您好,登錄后才能下訂單哦!
這篇文章主要介紹了.net微信開發(fā)中獲取AccessToken的示例,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
獲取AccessToken的方法,具體內(nèi)容如下
AccessToken獲取方法
public static Access_token GetAccessToken() { string formatString = String.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppId, AppSecret); Access_token res = new Access_token(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(formatString); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (retString.IndexOf("7200") > 0) { Access_token token = new Access_token(); token = JsonHelper.ParseFromJson<Access_token>(retString); res.access_token = token.access_token; res.expires_in = token.expires_in; } return res; }
Access_token類結(jié)構(gòu)
public class Access_token { public Access_token() { // //TODO:用于驗(yàn)證Access_token是否過期實(shí)體 // } string _access_token; string _expires_in; /// <summary> /// 獲取到的憑證 /// </summary> public string access_token { get { return _access_token; } set { _access_token = value; } } /// <summary> /// 憑證有效時(shí)間,單位:秒 /// </summary> public string expires_in { get { return _expires_in; } set { _expires_in = value; } } }
JsonHelper.ParseFromJson方法
/// <summary> /// 將JSON對象轉(zhuǎn)換為Model /// </summary> /// <typeparam name="T"></typeparam> /// <param name="szJson"></param> /// <returns></returns> public static T ParseFromJson<T>(string szJson) { T obj = Activator.CreateInstance<T>(); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); return (T)serializer.ReadObject(ms); } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“.net微信開發(fā)中獲取AccessToken的示例”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。