コード例 #1
0
ファイル: MornClient.cs プロジェクト: morningf/MornCore
        public T Execute <T>(MornRequest <T> request) where T : MornResponse
        {
            try
            {
                request.Validate();
            }
            catch (MornException ex)
            {
                return(CreateErrorResponse <T>(ex.ErrorType, ex.Message));
            }

            // 添加协议级请求参数
            Dictionary <string, string> txtParams = new Dictionary <string, string>();

            txtParams.Add(MornConstants.METHOD, request.GetApiName());
            txtParams.Add(MornConstants.TIMESTAMP, DateTime.Now.ToString(MornConstants.DATE_TIME_FORMAT));
            txtParams.Add(MornConstants.DATA, request.ToJson());
            if (!string.IsNullOrEmpty(AppKey))
            {
                txtParams.Add(MornConstants.APP_KEY, AppKey);
                // 添加签名参数
                if (!string.IsNullOrEmpty(AppSecret))
                {
                    txtParams.Add(MornConstants.SIGN, Security.Signature.SignRequestByMd5Method(txtParams, AppSecret));
                }
            }

            try
            {
                var data = JsonConvert.SerializeObject(txtParams);
                var body = _client.DoPost(ServerUrl, data);
                return(JsonConvert.DeserializeObject <T>(body));
            }
            catch (Exception ex)
            {
                return(CreateErrorResponse <T>(MornErrorType.SystemError, ex.Message));
            }
        }
コード例 #2
0
ファイル: MornClient.cs プロジェクト: morningf/MornCore
 public async Task <T> ExecuteAsync <T>(MornRequest <T> request) where T : MornResponse => await Task.Factory.StartNew(() => { return(Execute(request)); });