Example #1
0
        public static Result AnsyRequest(RestBag restBag)
        {
            Result re       = new Result();
            string sentJson = string.Empty;

            try
            {
                dynamic dy = new
                {
                    JsonBody           = restBag.req.JsonSerializer.ToString(),
                    RestSharpParameter = restBag.req.Parameters
                };
                var  dyJson = JsonConvert.SerializeObject(dy);
                bool writerequestlog;
                bool writeresponelog;
                SetIsWriteLog(restBag.uri, out writerequestlog, out writeresponelog);

                if (writerequestlog)
                {
                    LogService.Default.Trace("发送跟踪日志:" + dyJson + ",目标地址:" + restBag.uri);//日志记录
                }

                ApiUtil.RequestData(restBag);
            }
            catch (Exception ex)
            {
                re.code    = 0;
                re.message = ex.Message;
                LogService.Default.Fatal(ex);
            }
            return(re);
        }
Example #2
0
 private Result AnsyRequest(RestRequest rest, ExtendParams restExtendParams, string fullpath)
 {
     try
     {
         RestBag rrest = new RestBag();
         rrest.extendParms = restExtendParams;
         rrest.req         = rest;
         rrest.uri         = fullpath;
         Result r = RequestProvider.AnsyRequest(rrest);
         return(r);
     }
     finally
     {
     }
 }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestUri"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static Task <IRestResponse> RequestData(RestBag bag)
        {
            //处理timeout

            if (bag.extendParms == null || bag.extendParms.timeOut == 20000)
            {
                bag.req.Timeout = 7000;
            }
            else
            {
                bag.req.Timeout = bag.extendParms.timeOut;
            }
            string str = "";

            //处理version
            if (bag.extendParms == null)
            {
                str = "1.0.0.0";
            }
            else
            {
                str = bag.extendParms.version.ToSafeString("1.0.0.0");
            }

            //bag.req.AddHeader("Content-Type", "application/json; charset=utf-8");
            bag.req.AddHeader("s-version", str);

            try
            {
                bag.req.AddHeader("BTProcessInfo", Newtonsoft.Json.JsonConvert.SerializeObject(BTCallContextData.CurrentNlogTraceData));
            }
            catch (Exception ex)
            {
                LogService.Default.Fatal(ex, "post请求添加追踪日志异常:" + ex.Message + ex.StackTrace);
            }

            bag.req.UseDefaultCredentials = true;
            var restClient = new RestClient {
                BaseUrl = new Uri(bag.uri)
            };
            var tcs = new TaskCompletionSource <IRestResponse>();

            restClient.ExecuteAsync(bag.req, r => {
                tcs.SetResult(r);
            });
            return(tcs.Task);
        }
Example #4
0
        public static Result SyncRequest(RestBag restBag)
        {
            Result re       = new Result();
            string sentJson = string.Empty;
            bool   writerequestlog;
            bool   writeresponelog;

            SetIsWriteLog(restBag.uri, out writerequestlog, out writeresponelog);
            try
            {
                dynamic dy = new
                {
                    JsonBody           = restBag.req.JsonSerializer.ToString(),
                    RestSharpParameter = restBag.req.Parameters
                };
                var dyJson = JsonConvert.SerializeObject(dy);

                if (writerequestlog)
                {
                    LogService.Default.Trace("发送跟踪日志:" + dyJson + ",目标地址:" + restBag.uri);//日志记录
                }
                // 定义超时重试机制
                //var policy = Policy.HandleResult<IRestResponse>(r =>
                //{
                //    return r.StatusCode == HttpStatusCode.RequestTimeout || r.StatusCode == HttpStatusCode.GatewayTimeout || r.StatusCode == 0;
                //}).Or<TimeoutException>().Retry(3);

                //IRestResponse rjson = policy.Execute(() =>
                //{
                //    return ApiUtil.RequestData(restBag).Result;
                //});

                IRestResponse rjson = ApiUtil.RequestData(restBag).Result;
                if (writeresponelog)
                {
                    LogService.Default.Trace("返回跟踪日志:" + JsonConvert.SerializeObject("RestSharp_HTTP_StatusCode:" + rjson.StatusCode + "RestSharp_HTTP_Content:" + rjson.Content + "目标地址:" + restBag.uri));//日志记录););//日志记录
                }
                re.errorCode = ((int)rjson.StatusCode).ToSafeString();
                if ((int)rjson.StatusCode == 200)
                {
                    re.@object = rjson.Content;
                    re.code    = 1;
                }
                else
                {
                    re.@object = rjson.Content;
                    re.message = rjson.ErrorMessage ?? "";
                    if (string.IsNullOrWhiteSpace(re.message))
                    {
                        re.message = rjson.Content;
                    }
                    re.code = 0;
                    LogService.Default.Trace("返回跟踪错误响应日志:result: message" + re.message + "目标地址:" + restBag.uri);//日志记录);
                }
            }
            catch (Exception ex)
            {
                re.code    = 0;
                re.message = ex.Message;
                LogService.Default.Fatal(ex);
            }
            return(re);
        }