廣告:
微信的sdk文檔要求接入方緩存兩個(gè)參數(shù):access_token和jsapi_ticket,在一定時(shí)間內(nèi)這兩個(gè)參數(shù)的值是不會(huì)過期的,不需要每次請(qǐng)求微信服務(wù)器獲取。
參考以下文檔獲取access_token(有效期7200秒,開發(fā)者必須在自己的服務(wù)全局緩存access_token)
用第一步拿到的access_token采用http GET方式請(qǐng)求獲得jsapi_ticket(有效期7200秒,開發(fā)者必須在自己的服務(wù)全局緩存jsapi_ticket):https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="appId" value="" /> <add key="appSecret" value="" /> <add key="payApi" value="http://pay.53bk.com/" /> <add key="cacheApi" value="http://www.asiacable.com.cn/Api/WeixinCache" /> <add key="enableIP" value="127.0.0.1;127.0.0.1" /> </appSettings> </configuration>
<!--http://www.asiacable.com.cn/Api/WeixinCache 微信accesstoken、ticket跨域服務(wù)器全局緩存(公共緩存)接口-->
全局緩存接口如:http://www.asiacable.com.cn/Api/WeixinCache
文章后臺(tái)代碼:
#region WeixinCache() 服務(wù)器公共緩存接口 ---僅服務(wù)器端用 // /Api/WeixinCache?cachekey=accesstoken&hl=utf-8 /Api/WeixinCache?cachekey=ticket&hl=utf-8 public ActionResult WeixinCache() { Response.Cache.SetOmitVaryStar(true); // Request.ContentEncoding = Encoding.GetEncoding("utf-8"); string cachekey = ""; if (Request.QueryString["cachekey"] != null) cachekey = Request.QueryString["cachekey"].ToString(); if (Request.Form["cachekey"] != null) cachekey = Request.Form["cachekey"].ToString(); string charset = "utf-8"; if (Request.QueryString["hl"] != null) charset = Request.QueryString["hl"].ToString(); if (Request.Form["hl"] != null) charset = Request.Form["hl"].ToString(); if (charset == "utf-8") { Request.ContentEncoding = Encoding.GetEncoding("utf-8"); Response.ContentEncoding = Encoding.GetEncoding("utf-8"); } if (charset == "gb2312") { Request.ContentEncoding = Encoding.GetEncoding("gb2312"); Response.ContentEncoding = Encoding.GetEncoding("gb2312"); } if (charset == "gbk") { Request.ContentEncoding = Encoding.GetEncoding("gbk"); Response.ContentEncoding = Encoding.GetEncoding("gbk"); } string json1 = ""; Weixinbll wxbll = new Weixinbll(); if (cachekey=="accesstoken") { json1 = wxbll.getAccessTokenJsonstring(); } if (cachekey == "ticket") { json1 = wxbll.getJsApiTicketJsonstring(); } Response.Write(json1); return new EmptyResult(); } #endregion
最后程序后臺(tái)形成接口post代碼:
http://www.asiacable.com.cn/Api/WeixinCache?cachekey=accesstoken&hl=utf-8
廣告: