Beispiel #1
0
        /// <summary>
        /// 获取jsapi_ticket
        /// </summary>
        /// <returns></returns>
        public static string GetJsapiTicket()
        {
            string accessToken = GetAccessToken();
            string key         = "jsapi_ticket";

            if (CacheExtend.HasCache(key))
            {
                return(CacheExtend.GetCache <JsapiTicket>(key).ticket);
            }

            lock (_lock)
            {
                if (CacheExtend.HasCache(key))
                {
                    return(CacheExtend.GetCache <JsapiTicket>(key).ticket);
                }
                JsapiTicket ticket = null;
                try
                {
                    LogHelper.WriteLog("GetJsapiTicket-accessToken:" + accessToken);
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", accessToken));
                    req.Method = "GET";
                    using (WebResponse wr = req.GetResponse())
                    {
                        HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                        StreamReader    reader     = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                        string          content    = reader.ReadToEnd();
                        LogHelper.WriteLog("content:" + content);
                        if (!string.IsNullOrEmpty(content))
                        {
                            ticket = JsonHelper.JsonToObject <JsapiTicket>(content);
                            if (ticket != null)
                            {
                                LogHelper.WriteLog("ticket:" + ticket.ticket);
                                //LogHelper.WriteLog("jsapi_ticket插入缓存,jsapi_ticket有效期为:" + (ticket.expires_in - 1800).ToString());
                                CacheExtend.CacheInsert(key, ticket, DateTime.Now.AddSeconds(ticket.expires_in - 1800));
                            }
                            else
                            {
                                LogHelper.WriteLog(ErrorManage.GetErrorInfo(ticket.errcode ?? -1));
                            }
                        }
                        else
                        {
                            LogHelper.WriteLog("jsapi_ticket 返回的值为NULL");
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(string.Format("{0}\n{1}\n{2}\n", ex.Message, ex.Source, ex.StackTrace));
                }
                return(ticket == null ? "" : ticket.ticket);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            string key = "AccessToken";

            AccessToken token = null;

            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret));
                req.Method = "GET";
                using (WebResponse wr = req.GetResponse())
                {
                    HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                    StreamReader    reader     = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    string          content    = reader.ReadToEnd();

                    if (!string.IsNullOrEmpty(content))
                    {
                        token = JsonHelper.JsonToObject <AccessToken>(content);
                        if (token != null)
                        {
                            //LogHelper.WriteLog("Token插入缓存,Token有效期为:" + (token.expires_in - 1800).ToString());
                            CacheExtend.CacheInsert(key, token, DateTime.Now.AddSeconds(token.expires_in - 1800));
                        }
                        else
                        {
                            LogHelper.WriteLog(ErrorManage.GetErrorInfo(token.errcode ?? -1));
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog("Token 返回的值为NULL");
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(string.Format("{0}\n{1}\n{2}\n", ex.Message, ex.Source, ex.StackTrace));
            }
            return(token == null ? "" : token.access_token);
        }