.net MVC 微信公众号 获取 access_token
官方文档说明:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN
具体实现方法如下:
类WXApi.cs:
#region 获取access_token /// <summary> /// 获取access_token /// </summary> public static string GetAccessToken(string appid, string secret) { string strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret)); return Tools.GetJsonValue(strJson, "access_token"); } #endregion
类AdminUtil.cs:
#region 获取access_token /// <summary> /// 获取access_token /// </summary> public static string GetAccessToken() { string access_token = WXModel.access_token; string AppID = WXModel.AppID; string AppSecret = WXModel.AppSecret; //获取session 中的 access_token if (string.IsNullOrWhiteSpace(access_token)) //尚未保存过access_token { access_token = WXApi.GetAccessToken(AppID, AppSecret); } else { if (WXApi.TokenExpired(access_token)) //access_token过期 { access_token = WXApi.GetAccessToken(AppID, AppSecret); } else { return access_token; } } //更新access_token HttpContext.Current.Application["access_token"] = access_token; return access_token; } #endregion
Controller:
public void GetAccessToken() { string access_token = AdminUtil.GetAccessToken(); Response.Write("access_token=" + access_token); }
工具类:
public static class WXModel { public static string access_token; public static string AppID; public static string AppSecret; public static string mch_id; public static string key; }
public class HttpRequestUtil { #region 请求Url,不发送数据 /// <summary> /// 请求Url,不发送数据 /// </summary> public static string RequestUrl(string url) { return RequestUrl(url, "POST"); } #endregion #region 请求Url,不发送数据 /// <summary> /// 请求Url,不发送数据 /// </summary> public static string RequestUrl(string url, string method) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = method; request.ContentType = "text/html"; request.Headers.Add("charset", "utf-8"); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.Default); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } #endregion #region 请求Url,发送json数据 /// <summary> /// 请求Url,发送json数据 /// </summary> public static string RequestUrlSendMsg(string url, string method,string JSONData) { byte[] bytes = Encoding.UTF8.GetBytes(JSONData); // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = method; request.ContentType = "text/html"; request.Headers.Add("charset", "utf-8"); Stream reqstream = request.GetRequestStream(); reqstream.Write(bytes, 0, bytes.Length); //声明一个HttpWebRequest请求 request.Timeout = 90000; //设置连接超时时间 request.Headers.Set("Pragma", "no-cache"); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.Default); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } #endregion }