Ejemplo n.º 1
0
        public void DigestWithApiKeyTest()
        {
            const string httpMethod = "POST";
            const string url        = "http://channel.api.duapp.com/rest/2.0/channel/channel";
            var          paramDic   = new Dictionary <string, string>
            {
                { "device_type", "3" },
                { "messages", "{\"title\":\"123\",\"description\":\"123abc\"}" },
                { "msg_keys", "channel_msg_key" },
                { "method", "push_msg" },
                { "timestamp", "1376050316" },
                { "push_type", "3" },
                { "message_type", "0" },
                { "apikey", "Q7rF8EqTV03w4W1fM8gdF0pn" }
            };

            string sign = SignatureDigest.Digest(httpMethod, url, paramDic, _apiKey, _secretKey);

            Assert.NotNull(sign);
            //Assert.AreEqual("0313347b73fcbe71bf6f48e4154dac9f", sign);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 生成待POST的数据
        /// </summary>
        /// <param name="apiKey"></param>
        /// <param name="secretKey"></param>
        /// <returns></returns>
        public virtual string GeneratePostData(string apiKey, string secretKey)
        {
            if (string.IsNullOrEmpty(apiKey))
            {
                throw new ArgumentNullException("apiKey", "ApiKey MUST set");
            }
            if (string.IsNullOrEmpty(secretKey))
            {
                throw new ArgumentNullException("secretKey", "SecretKey MUST set");
            }

            var paramDic = new Dictionary <string, string>();

            var propertyInfos = GetType().GetProperties();

            foreach (var propertyInfo in propertyInfos)
            {
                var value        = propertyInfo.GetValue(this, null);
                var customAttres = propertyInfo.GetCustomAttributes(typeof(RequestKeyAttribute), true);
                if (customAttres.Length > 1)
                {
                    throw new ArgumentNullException(propertyInfo.Name, "Can NOT set multi RequestKeyAttribute");
                }
                if (customAttres.Length == 1)
                {
                    var customAttribute     = customAttres[0];
                    var requestKeyAttribute = customAttribute as RequestKeyAttribute;
                    if (requestKeyAttribute == null)
                    {
                        throw new ArgumentNullException(propertyInfo.Name, "Use as convert to RequestKeyAttribute Fail");
                    }
                    if (requestKeyAttribute.IsReqired && value == null)
                    {
                        throw new ArgumentNullException(propertyInfo.Name);
                    }
                    if (value != null)
                    {
                        paramDic.Add(requestKeyAttribute.KeyName, RequestKeyConverter.ConvertDataToString(value));
                    }
                }
            }

            var sign = SignatureDigest.Digest("POST", ServiceUrl, paramDic, apiKey, secretKey);

            if (string.IsNullOrEmpty(sign))
            {
                throw new Exception("sign is null, may Compute sign Digest Fail");
            }

            paramDic.Add("apikey", apiKey);
            paramDic.Add("sign", sign);
            var paramList = new List <string>(paramDic.Count);

            paramList.AddRange(paramDic.Select(paramKv => string.Format("{0}={1}&", paramKv.Key, Uri.EscapeDataString(paramKv.Value))));
            paramList.Sort();
            var baseData = new StringBuilder();

            foreach (var param in paramList)
            {
                baseData.Append(param);
            }
            if (baseData[baseData.Length - 1] == '&')
            {
                baseData.Remove(baseData.Length - 1, 1);
            }

            string result = baseData.ToString();

            return(result);
        }