/// <summary>
        /// 缓存并获取调用微信公众号接口的全局唯一票据
        /// </summary>
        /// <param name="appId">设置应用的APPID</param>
        /// <param name="secret">设置应用的APPSECRET</param>
        /// <returns>如果缓存的票据有效,则返回缓存的票据字符串,否则调用接口重新获取票据信息</returns>
        public static string GetAccessToken(string appId, string secret)
        {
            HttpApplicationState apps = HttpContext.Current.Application;
            SortedDictionary<string, object> cache = null;
            string accessToken = null;

            if (apps[CACHEKEYNAME] != null)
                cache = apps[CACHEKEYNAME] as SortedDictionary<string, object>;

            if (!General.IsNullable(cache))
            {
                DateTime saveTime = Convert.ToDateTime(cache["last_save_time"]);
                int cost = (int)(DateTime.Now - saveTime).TotalSeconds;
                int expires = Convert.ToInt32(cache["expires_in"]);
                if (cost < expires)
                    accessToken = cache["access_token"].ToString();
            }

            if (General.IsNullable(accessToken))
            {
                if (!General.IsNullable(appId) && !General.IsNullable(secret))
                {
                    string url = "https://api.weixin.qq.com/cgi-bin/token";
                    WxParameters paras = new WxParameters();
                    paras.SetValue("grant_type", "client_credential");
                    paras.SetValue("appid", appId);
                    paras.SetValue("secret", secret);

                    NetClient client = new NetClient();
                    string response = client.Get(url, paras.Parameters);
                    WxParameters result = new WxParameters();

                    if (result.FromJson(response))
                    {
                        if (result.ContainsValue("access_token") && result.ContainsValue("expires_in"))
                        {
                            cache = result.Parameters;
                            cache.Add("last_save_time", DateTime.Now);
                            apps[CACHEKEYNAME] = cache;
                            accessToken = result["access_token"] as string;
                        }
                    }
                }
            }

            return accessToken;
        }
        /// <summary>
        /// 通过登录授权的验证信息凭证获取用户个人信息
        /// </summary>
        /// <param name="accessToken">设置登录授权的验证信息凭证</param>
        /// <param name="userId">设置用户唯一标识,一般为用户的OpenId,如果使用了微信的开放平台则为UnionId</param>
        /// <returns>返回授权后获取到的用户个人信息</returns>
        public static UserInfoResults GetUserInfoResults(string accessToken, string userId)
        {
            UserInfoResults results = null;

            if (!General.IsNullable(accessToken) && !General.IsNullable(userId))
            {
                string url = "https://api.weixin.qq.com/sns/userinfo?";

                WxParameters paras = new WxParameters();
                paras.SetValue("access_token", accessToken);
                paras.SetValue("openid", userId);

                NetClient client = new NetClient();
                string json = client.Get(url, paras.Parameters);
                results = UserInfoResults.Pares(json);
            }

            return results;
        }
        /// <summary>
        /// 通过code获取验证信息凭证
        /// </summary>
        /// <param name="appId">设置应用的唯一标识</param>
        /// <param name="secret">设置应用密钥,这个密钥是在微信开放平台提交应用审核通过后获得的</param>
        /// <param name="code">设置微信服务端返回的code字符串</param>
        /// <returns>返回微信服务端响应的验证信息凭证</returns>
        public static AuthorizationResults GetAccessTokenResults(string appId, string secret, string code)
        {
            AuthorizationResults results = null;

            if (!General.IsNullable(appId) && !General.IsNullable(code) && !General.IsNullable(secret))
            {
                string url = "https://api.weixin.qq.com/sns/oauth2/access_token?";

                WxParameters paras = new WxParameters();
                paras.SetValue("appid", appId);
                paras.SetValue("secret", secret);
                paras.SetValue("code", code);
                paras.SetValue("grant_type", "authorization_code");

                NetClient client = new NetClient();
                string json = client.Get(url, paras.Parameters);

                results = AuthorizationResults.Pares(json);
            }

            return results;
        }
Exemple #4
0
        private static string GetTicket(string appId, string secret)
        {
            HttpApplicationState apps = HttpContext.Current.Application;
            SortedDictionary<string, object> cache = null;
            string ticket = null;

            if (apps[CACHEKEYNAME] != null)
                cache = apps[CACHEKEYNAME] as SortedDictionary<string, object>;

            if (!General.IsNullable(cache))
            {
                DateTime saveTime = Convert.ToDateTime(cache["last_save_time"]);
                int cost = (int)(DateTime.Now - saveTime).TotalSeconds;
                int expires = Convert.ToInt32(cache["expires_in"]);
                if (cost < expires)
                    ticket = cache["ticket"] as string;
            }

            if (General.IsNullable(ticket))
            {
                if (!General.IsNullable(appId) && !General.IsNullable(secret))
                {
                    string accessToken = InterfaceCredentials.GetAccessToken(appId, secret);
                    string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
                    WxParameters paras = new WxParameters();
                    paras.SetValue("access_token", accessToken);
                    paras.SetValue("type", "jsapi");

                    NetClient client = new NetClient();
                    string response = client.Get(url, paras.Parameters);
                    WxParameters result = new WxParameters();

                    if (result.FromJson(response))
                    {
                        if (result.ContainsValue("ticket") && result.ContainsValue("expires_in"))
                        {
                            cache = result.Parameters;
                            cache.Remove("errcode");
                            cache.Remove("errmsg");
                            cache.Add("last_save_time", DateTime.Now);
                            apps[CACHEKEYNAME] = cache;
                            ticket = result["ticket"] as string;
                        }
                    }
                }
            }

            return ticket;
        }
 /// <summary>
 /// 使用当前参数集调用指定的API并指定参数的提交格式
 /// </summary>
 /// <param name="url">设置要调用的API地址</param>
 /// <param name="contentType">设置Content-typeHTTP标头值,默认为text/xml</param>
 /// <returns>返回服务器响应的内容</returns>
 public virtual string Post(string url, string contentType = MimeType.XML)
 {
     string content = (contentType.Equals(MimeType.XML) ? this.ToXML() : this.ToJson());
     NetClient client = new NetClient();
     client.ContentType = contentType;
     //client.Proxy = new System.Net.WebProxy("http://10.152.18.220:8080");
     return client.Post(url, content);
 }
        /// <summary>
        /// 获取微信服务器的IP地址列表
        /// </summary>
        /// <param name="accessToken">设置全局接口调用票据字符串</param>
        /// <returns>如果票据有效,则返回微信服务器的IP地址列表,否则返回NULL</returns>
        public static List<string> GetServerIP(string accessToken)
        {
            List<string> ipList = null;

            if (!General.IsNullable(accessToken))
            {
                string url = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
                NetClient client = new NetClient();
                string response = client.Get(url, new string[] { "access_token=" + accessToken });
                WxParameters result = new WxParameters();
                if (result.FromJson(response))
                {
                    if (result.ContainsValue("ip_list"))
                        ipList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(result["ip_list"].ToString());
                }
            }

            return ipList;
        }
Exemple #7
0
        /// <summary>
        /// 批量发送短信
        /// </summary>
        /// <param name="mobileNumbers">接收短信内容的手机号码列表</param>
        /// <param name="content">短信的内容</param>
        /// <param name="userName">调用服务所需的用户名</param>
        /// <param name="password">调用服务所需的密码</param>
        /// <param name="otherParameters">调用服务所需提交的其它参数</param>
        /// <returns>返回服务器响应的文本内容</returns>
        public virtual string Send(List<string> mobileNumbers, string content, string userName, string password, Dictionary<string, string> otherParameters = null)
        {
            mobileNumbers = this.FilterNumbers(mobileNumbers);
            content = this.EncodeContent(content);

            if (
                General.IsNullable(mobileNumbers)
             || General.IsNullable(content)
             || General.IsNullable(userName)
             || General.IsNullable(password)
            )
                return null;

            NetClient cilent = new NetClient();
            string method = this._sendFunction.Method;
            string requestUri = this.Host + this._sendFunction.FunctionName;
            SmsSendFunction.SmsRequestParameterCollection parameters = this._sendFunction.Parameters;

            string request = string.Format(
                "{0}={1}&{2}={3}&{4}={5}&{6}={7}",
                this._sendFunction.UserNameParameterName,
                userName,
                this._sendFunction.PasswordParameterName,
                HttpUtility.UrlEncode(password, this._encoding),
                this._sendFunction.MobileParameterName,
                string.Join(",", mobileNumbers),
                this._sendFunction.ContentParameterName,
                content
            );

            if (!General.IsNullable(parameters))
            {
                bool newValue = !General.IsNullable(otherParameters);

                for (int i = 0, c = parameters.Count; i < c; i++)
                {
                    SmsRequestParameter p = parameters[i];
                    string name = p.Name;
                    string value = p.DefaultValue;
                    if (newValue && otherParameters.ContainsKey(name))
                        value = otherParameters[name];

                    if (!General.IsNullable(value))
                        request += string.Format("&{0}={1}", name, value);
                }
            }

            return cilent.Submit(requestUri, method, this.ContentEncoding, request);
        }