コード例 #1
0
    /// <summary>
    /// 获取ACCESS_TOKEN方法
    /// </summary>
    /// <param name="appId"></param>
    /// <param name="appSecret"></param>
    /// <returns></returns>
    private static string GetAccessToken(string appId, string appSecret)
    {
        string url    = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appId, appSecret);
        string result = WeChatHelper.GetData(url);

        if (result.Contains("40164"))
        {
            LogHelper.Write("TokenModel_GetAccessToken出错:" + result);
            return(null);
        }

        XmlDocument doc  = JsonHelper.ParseJson(result, "root");
        XmlNode     root = doc.SelectSingleNode("root");

        if (root != null)
        {
            XmlNode access_token = root.SelectSingleNode("access_token");
            if (access_token != null)
            {
                GetAccessToken_Time = DateTime.Now;
                if (root.SelectSingleNode("expires_in") != null)
                {
                    Expires_Period = int.Parse(root.SelectSingleNode("expires_in").InnerText);
                }
                return(access_token.InnerText);
            }
            else
            {
                GetAccessToken_Time = DateTime.MinValue;
            }
        }

        return(null);
    }
コード例 #2
0
        /// <summary>
        /// 获取Jsapi_ticket方法
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="appSecret"></param>
        /// <returns></returns>
        private static string GetJsapi_ticket(string appId, string appSecret)
        {
            string url    = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", TokenModel.AccessToken);
            string result = WeChatHelper.GetData(url);

            XmlDocument doc  = JsonHelper.ParseJson(result, "root");
            XmlNode     root = doc.SelectSingleNode("root");

            if (root != null)
            {
                XmlNode ticket = root.SelectSingleNode("ticket");
                if (ticket != null)
                {
                    GetJsapi_ticket_Time = DateTime.Now;
                    if (root.SelectSingleNode("expires_in") != null)
                    {
                        Jsapi_ticket_Expires_Period = int.Parse(root.SelectSingleNode("expires_in").InnerText);
                    }
                    return(ticket.InnerText);
                }
                else
                {
                    GetJsapi_ticket_Time = DateTime.MinValue;
                }
            }

            return(null);
        }