Exemplo n.º 1
0
 public string GetMimeType()
 {
     if (this.mimeType == null)
     {
         this.mimeType = JdUtils.GetMimeType(GetContent());
     }
     return(this.mimeType);
 }
Exemplo n.º 2
0
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
     if (value is DateTime)
     {
         DateTime dateTime    = (DateTime)value;
         String   dateTimeStr = JdUtils.FormatDateTime(dateTime);
         writer.WriteValue(dateTimeStr);
     }
 }
Exemplo n.º 3
0
        public virtual String GetParamJson()
        {
            var paramters = new Dictionary <String, Object>();

            foreach (var added in _addedParamters)
            {
                if (!paramters.ContainsKey(added.Key))
                {
                    paramters.Add(added.Key, added.Value);
                }
            }

            return(JsonConvert.SerializeObject(paramters, JdUtils.GetJsonConverters()));
        }
Exemplo n.º 4
0
        private T DoExecute <T>(IJdRequest <T> request, string accessToken, DateTime timestamp) where T : JdResponse
        {
            //if (String.IsNullOrEmpty(accessToken))
            //{
            //    throw new Exception("Access Token 必须提供!");
            //}

            // 提前检查业务参数
            try
            {
                request.Validate();
            }
            catch (JdException e)
            {
                return(createErrorResponse <T>(e.ErrorCode, e.ErrorMsg));
            }

            // 添加协议级请求参数
            JdDictionary txtParams = new JdDictionary();

            txtParams.Add(APP_PARAM, request.GetParamJson());
            txtParams.Add(METHOD, request.ApiName);
            txtParams.Add(VERSION, "2.0");
            txtParams.Add(APP_KEY, appKey);
            //txtParams.Add(FORMAT, format);
            //txtParams.Add(PARTNER_ID, "top-sdk-net-20111024");
            txtParams.Add(TIMESTAMP, timestamp);
            txtParams.Add(ACCESSTOKEN, accessToken);

            // 添加签名参数
            txtParams.Add(SIGN, JdUtils.SignJdRequest(txtParams, appSecret));

            // 是否需要上传文件
            string body;

            if (request is IJdUploadRequest <T> )
            {
                IJdUploadRequest <T>           uRequest   = (IJdUploadRequest <T>)request;
                IDictionary <string, FileItem> fileParams = JdUtils.CleanupDictionary(uRequest.GetFileParameters());
                body = webUtils.DoPost(this.serverUrl, txtParams, fileParams);
            }
            else
            {
                body = webUtils.DoPost(this.serverUrl, txtParams);
            }

            // 解释响应结果
            T rsp;

            if (disableParser)
            {
                rsp      = Activator.CreateInstance <T>();
                rsp.Body = body;
            }
            else
            {
                if (FORMAT_JSON.Equals(format))
                {
                    IJdParser <T> tp = new JdJsonParser <T>();
                    rsp = tp.Parse(body);
                }
                else
                {
                    IJdParser <T> tp = new JdXmlParser <T>();
                    rsp = tp.Parse(body);
                }
            }

            // 追踪错误的请求
            if (!disableTrace)
            {
                rsp.ReqUrl = webUtils.BuildGetUrl(this.serverUrl, txtParams);
                if (rsp.IsError)
                {
                    topLogger.Warn(rsp.ReqUrl + "\r\n" + rsp.Body);
                }
            }

            return(rsp);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 添加一个新的键值对。空键或者空值的键值对将会被忽略。
        /// </summary>
        /// <param name="key">键名称</param>
        /// <param name="value">键对应的值,目前支持:string, int, long, double, bool, DateTime类型</param>
        public void Add(string key, object value)
        {
            string strValue;

            if (value == null)
            {
                strValue = null;
            }
            else if (value is string)
            {
                strValue = (string)value;
            }
            else if (value is DateTime)
            {
                strValue = JdUtils.FormatDateTime((DateTime)value);
            }
            else if (value is Boolean)
            {
                strValue = ((Boolean)value).ToString().ToLower();
            }
            else if (value is Nullable <DateTime> )
            {
                Nullable <DateTime> dateTime = value as Nullable <DateTime>;
                strValue = dateTime.HasValue ? JdUtils.FormatDateTime(dateTime.Value) : null;
            }
            else if (value is Nullable <Int16> )
            {
                Nullable <Int16> v = value as Nullable <Int16>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable <Int32> )
            {
                Nullable <Int32> v = value as Nullable <Int32>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable <Int64> )
            {
                Nullable <Int64> v = value as Nullable <Int64>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable <Single> )
            {
                Nullable <Single> v = value as Nullable <Single>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable <Decimal> )
            {
                Nullable <Decimal> v = value as Nullable <Decimal>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable <Double> )
            {
                Nullable <Double> v = value as Nullable <Double>;
                strValue = v.HasValue ? v.Value.ToString() : null;
            }
            else if (value is Nullable <Boolean> )
            {
                Nullable <Boolean> v = value as Nullable <Boolean>;
                strValue = v.HasValue ? v.Value.ToString().ToLower() : null;
            }
            else if (value is List <JdSdk.Request.Field> )
            {
                List <JdSdk.Request.Field> v = value as List <JdSdk.Request.Field>;
                strValue = ParseFieldList(v);
            }
            else if (value is Byte[])
            {
                strValue = Convert.ToBase64String((Byte[])value);
            }
            else
            {
                strValue = value.ToString();
            }

            this.Add(key, strValue);
        }