Beispiel #1
0
        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="paramValue">参数值:如果为集合或数组类型,则自动拆解,最终想作为数组提交到服务端</param>
        /// <param name="ignoreSign">是否忽略签名</param>
        /// <returns></returns>
        public YopRequest addParam(string paramName, object paramValue, bool ignoreSign)
        {
            Assert.hasText(paramName, "参数名不能为空");
            if (paramValue == null ||
                ((paramValue is string) && StringUtils.isBlank((string)paramValue)) ||
                ((paramValue is IDictionary) && ((IDictionary)paramValue).Count == 0))
            {
                //logger.warn("参数" + paramName + "为空,忽略");
                return(this);
            }
            if (YopConstants.isProtectedKey(paramName))
            {
                paramMap.Set(paramName, paramValue.ToString().Trim());
                return(this);
            }
            if (paramValue is IDictionary)
            {
                // 集合类
                foreach (object o in (IDictionary)paramValue)
                {
                    if (o != null)
                    {
                        paramMap.Add(paramName, o.ToString().Trim());
                    }
                }
            }
            else if (paramValue is Array)
            {
                // 数组
                int len = (paramValue as Array).Length;
                for (int i = 0; i < len; i++)
                {
                    object o = (paramValue as Array).GetValue(i);// Array.get(paramValue, i);
                    if (o != null)
                    {
                        paramMap.Add(paramName, o.ToString().Trim());
                    }
                }
            }
            else
            {
                paramMap.Add(paramName, paramValue.ToString().Trim());
            }

            if (ignoreSign)
            {
                ignoreSignParams.Add(paramName);
            }
            return(this);
        }
Beispiel #2
0
        /**
         * 请求加密,使用AES算法,要求secret为正常的AESkey
         *
         * @throws Exception
         */
        protected static void encrypt(YopRequest request)
        {
            StringBuilder       builder  = new StringBuilder();
            bool                first    = true;
            NameValueCollection myparams = request.getParams();

            foreach (string key in myparams.AllKeys)
            {
                if (YopConstants.isProtectedKey(key))
                {
                    continue;
                }

                string[]      strValues = myparams.GetValues(key);
                List <string> values    = new List <string>();
                foreach (string s in strValues)
                {
                    values.Add(s);
                }
                myparams.Remove(key);
                if (values == null || values.Count == 0)
                {
                    continue;
                }
                foreach (string v in values)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        builder.Append("&");
                    }
                    // 避免解密后解析异常,此处需进行encode(此逻辑在整个request做encoding前)
                    builder.Append(key).Append("=").Append(HttpUtility.UrlEncode(v, Encoding.UTF8));//YopConstants.ENCODING
                }
            }
            string encryptBody = builder.ToString();

            if (StringUtils.isBlank(encryptBody))
            {
                // 没有需加密的参数,则只标识响应需加密
                request.addParam(YopConstants.ENCRYPT, true);
            }
            else
            {
                if (StringUtils.isNotBlank(request
                                           .getParamValue(YopConstants.APP_KEY)))
                {
                    // 开放应用使用AES加密
                    string encrypt = AESEncrypter.encrypt(encryptBody,
                                                          request.getSecretKey());
                    request.addParam(YopConstants.ENCRYPT, encrypt);
                }
                else
                {
                    // 商户身份调用使用Blowfish加密
                    string encrypt = BlowFish.Encrypt(encryptBody,
                                                      request.getSecretKey());

                    request.addParam(YopConstants.ENCRYPT, encrypt);
                }
            }
        }