Exemplo n.º 1
0
        public static WechatJsTicket RefrenshJsapi_ticket(string access_token, string userAgent)
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token);

            HttpWebResponse response = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);

            //Stream stream = response.GetResponseStream();
            //StreamReader sr = new StreamReader(stream);
            //string result = sr.ReadToEnd();
            string         result = WechatHttpWebResponseUtility.HttpResponseToString(response);
            WechatJsTicket ticket = JsonConvert.DeserializeObject <WechatJsTicket>(result);

            if (string.IsNullOrEmpty(ticket.ticket))
            {
                WechatError err = new WechatError();
                err = JsonConvert.DeserializeObject <WechatError>(result);
                string error = "appid或者appsecret错误,无法获取access_token 微信错误代码:" + err.errcode + "微信错误信息:" + err.errmsg;
                WechatLogHelper.Error(error);
            }
            else
            {
                string ticketExpiredTime = System.DateTime.Now.AddMinutes(115).ToString("yyyy-MM-dd HH:mm:ss");

                ConfigTools.WriteKey("WechatJsapi_ticket", ticket.ticket);
                ConfigTools.WriteKey("WechatJsapi_ticketExpiredTime", ticketExpiredTime);
            }


            return(ticket);
        }
Exemplo n.º 2
0
        //刷新accesstoken
        public static string  RefrenshToken(string appid, string appsecret)
        {
            string userAgent = System.Web.HttpContext.Current.Request.UserAgent;
            string url       = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, appsecret);

            HttpWebResponse res    = WechatHttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);
            Stream          stream = res.GetResponseStream();
            StreamReader    sr     = new StreamReader(stream);
            string          result = sr.ReadToEnd();

            WechatAccessToken token = new WechatAccessToken();

            token = JsonConvert.DeserializeObject <WechatAccessToken>(result);
            string access_token = token.access_token;

            if (string.IsNullOrEmpty(token.access_token))
            {
                WechatError err = new WechatError();
                err          = JsonConvert.DeserializeObject <WechatError>(result);
                access_token = "appid或者appsecret错误,无法获取access_token 微信错误代码:" + err.errcode + "微信错误信息:" + err.errmsg;
                WechatLogHelper.Error(access_token);
            }
            else
            {
                ConfigTools.WriteKey("WechatAccessToken", access_token);
                ConfigTools.WriteKey("WechatAccessTokenExpiredTime", System.DateTime.Now.AddMinutes(115).ToString("yyyy-MM-dd HH:mm:ss"));
            }
            return(access_token);
        }