Ejemplo n.º 1
0
 private void Validate <T>(IPacRequest <T> request, string fromCode, string toCode) where T : PacResponse
 {
     if (request == null)
     {
         throw new PacException("请求数据对象request不能为空");
     }
     if (string.IsNullOrEmpty(fromCode))
     {
         throw new PacException("fromCode为空");
     }
 }
Ejemplo n.º 2
0
 public T Send <T>(IPacRequest <T> request, string fromCode, string toCode) where T : PacResponse
 {
     return(DoSend <T>(request, fromCode, toCode));
 }
Ejemplo n.º 3
0
 public T Send <T>(IPacRequest <T> request, string fromCode) where T : PacResponse
 {
     return(Send(request, fromCode, string.Empty));
 }
Ejemplo n.º 4
0
        private T DoSend <T>(IPacRequest <T> request, string fromCode, string toCode) where T : PacResponse
        {
            // 传入参数验证
            try
            {
                Validate(request, fromCode, toCode);
            }
            catch (PacException e)
            {
                return(CreateErrorResponse <T>(e.ErrorCode, e.ErrorMsg));
            }

            // 序列化报文
            var content = SerializeHelp.SerializeByRef(request);
            //// 反序列化报文
            //var model = SerializeHelper.DesrializeByRef(content, typeof(T)) as T;

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

            txtParams.Add("msg_type", request.GetApi());
            txtParams.Add("logistics_interface", content);
            txtParams.Add("data_digest", PacUtils.SignSdkRequest(content, request, appSecret)); // 添加签名参数
            txtParams.Add("logistic_provider_id", fromCode);
            if (!string.IsNullOrEmpty(toCode))
            {
                txtParams.Add("to_code", toCode);
            }

            //发送请求
            string responseText = string.Empty;
            var    rspModel     = Activator.CreateInstance <T>();

            try
            {
                responseText     = webUtils.DoPost(serverUrl, txtParams);
                rspModel.Success = true;
                rspModel         = SerializeHelper.DesrializeByRef(responseText, typeof(T)) as T;
            }
            catch (Exception exception)
            {
                rspModel.Success   = false;
                rspModel.ErrorCode = "S99";
                rspModel.ErrorMsg  = "错误:" + exception.Message;
            }

            return(rspModel);


            //try
            //{
            //    string body;
            //    //if (request is ITopUploadRequest<T>) // 是否需要上传文件
            //    //{
            //    //    ITopUploadRequest<T> uRequest = (ITopUploadRequest<T>)request;
            //    //    IDictionary<string, FileItem> fileParams = TopUtils.CleanupDictionary(uRequest.GetFileParameters());
            //    //    body = webUtils.DoPost(this.serverUrl, txtParams, fileParams);
            //    //}
            //    //else
            //    //{
            //        body = webUtils.DoPost(serverUrl, txtParams);
            //    //}

            //   // 解释响应结果
            //   var rsp = Activator.CreateInstance<T>();
            //    rsp.
            //    //if (disableParser)
            //    //{
            //    //    rsp = Activator.CreateInstance<T>();
            //    //    rsp.Body = body;
            //    //}
            //    //else
            //    //{
            //    //    if (FORMAT_XML.Equals(format))
            //    //    {
            //    //        ITopParser tp = new TopXmlParser();
            //    //        rsp = tp.Parse<T>(body);
            //    //    }
            //    //    else
            //    //    {
            //    //        ITopParser tp = new TopJsonParser();
            //    //        rsp = tp.Parse<T>(body);
            //    //    }
            //    //}

            //    //// 追踪错误的请求
            //    //if (!disableTrace && rsp.IsError)
            //    //{
            //    //    StringBuilder sb = new StringBuilder(reqUrl).Append(" response error!\r\n").Append(rsp.Body);
            //    //    _pacLogger.Warn(sb.ToString());
            //    //}
            //    return rsp;
            //}
            //catch (Exception e)
            //{
            //    if (!disableTrace)
            //    {
            //        //StringBuilder sb = new StringBuilder(reqUrl).Append(" request error!\r\n").Append(e.StackTrace);
            //        //_pacLogger.Error(sb.ToString());
            //    }
            //    throw e;
            //}
        }