Example #1
0
        public async void Test_Track()
        {
            var request = new KdniaoTrackQuery()
            {
                LogisticCode = "",
                ShipperCode  = "YTO",
            };

            //var result = await _client.TracKAsync(request);
            //Output.WriteLine(JsonConvert.SerializeObject(result));
        }
Example #2
0
        /// <summary>
        /// 即时查询
        /// </summary>
        /// <param name="request">即时查询请求</param>
        /// <returns></returns>
        public async Task <KdniaoTrackQueryResult> TracKAsync(KdniaoTrackQuery request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrWhiteSpace(_account.MerchantId))
            {
                throw new ArgumentNullException(nameof(_account.MerchantId));
            }
            if (string.IsNullOrWhiteSpace(_account.ApiKey))
            {
                throw new ArgumentNullException(nameof(_account.ApiKey));
            }

            request.CheckParameters();

            var json = JsonConvert.SerializeObject(request);

            var bizParams = new SortedDictionary <string, string>()
            {
                // 请求内容需要进行URL(utf-8)编码。请求内容JSON格式,须和DataType一致
                { "RequestData", HttpUtility.UrlEncode(json, Encoding.UTF8) },
                //商户ID,请在我的服务页面查看
                { "EBusinessID", _account.MerchantId },
                // 请求指令类型
                { "RequestType", ((int)KdniaoRequestType.Track).ToString() },
                // 数据内容签名。把(请求内容(未编码)+AppKey)进行MD5加密,然后Base64编码,最后进行URL(utf-8)编码
                { "DataSign", HttpUtility.UrlEncode(Sign(json, _account.ApiKey), Encoding.UTF8) },
                // 请求、返回数据类型那:2=json
                { "DataType", "2" },
            };

            var content = new FormUrlEncodedContent(bizParams);

            return(await _proxy.TrackAsync(content)
                   .Retry(_config.RetryTimes)
                   .Handle()
                   .WhenCatch <Exception>(e =>
            {
                _exceptionHandler?.Invoke(e);
                return ReturnAsDefaultResponse();
            }));
        }