相思资源网 Design By www.200059.com
本文实例为大家分享了获取AccessToken的方法,供大家参考,具体内容如下
AccessToken获取方法
public static Access_token GetAccessToken() { string formatString = String.Format("https://api.weixin.qq.com/cgi-bin/token", 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类结构
public class Access_token { public Access_token() { // //TODO:用于验证Access_token是否过期实体 // } string _access_token; string _expires_in; /// <summary> /// 获取到的凭证 /// </summary> public string access_token { get { return _access_token; } set { _access_token = value; } } /// <summary> /// 凭证有效时间,单位:秒 /// </summary> public string expires_in { get { return _expires_in; } set { _expires_in = value; } } }
JsonHelper.ParseFromJson方法
/// <summary> /// 将JSON对象转换为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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
微信,AccessToken
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无.net微信开发 如何获取AccessToken的评论...