Esempio n. 1
0
        private static HttpPostCfg CreateDefaultHttpPostCfg()
        {
            var httpCfg = new HttpPostCfg
            {
                ReTryCount  = 3,
                ContentType = ContentType.Json,
            };

            return(httpCfg);
        }
Esempio n. 2
0
        public static string HttpPost(this string url, Dictionary <string, object> postParams, HttpPostCfg httpCfg = null)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                throw new FException("url不能为空");
            }
            if (postParams == null || !postParams.Any())
            {
                throw new FException("postParams不能为空");
            }

            if (httpCfg == null)
            {
                httpCfg = CreateDefaultHttpPostCfg();
            }
            else if (httpCfg.ReTryCount <= 0)
            {
                httpCfg.ReTryCount = 1;//至少执行一次
            }

            var restRequest = new RestRequest(Method.POST);

            restRequest.Timeout = 10 * 1000;//连接超时设置为10秒

            restRequest.AddHeader("Accept", "*/*");
            string contentType = GetContentType(httpCfg.ContentType);

            restRequest.AddHeader("Content-Type", contentType);

            if (httpCfg.ContentType == ContentType.Json)
            {
                restRequest.AddJsonBody(postParams);
            }
            else if (httpCfg.ContentType == ContentType.Text)
            {
                string queryString = postParams.GetUrlParams();
                restRequest.AddParameter(contentType, queryString, ParameterType.RequestBody);
            }
            else if (httpCfg.ContentType == ContentType.Form)
            {
                foreach (var param in postParams)
                {
                    restRequest.AddParameter(param.Key, param.Value);
                }
            }

            //如果是网络连接异常,那么重试
            IRestResponse restResponse = null;

            CallHelper.ReTryRun(reTryCount: httpCfg.ReTryCount, reTryAction: () =>
            {
                restResponse         = new RestClient(url).ExecuteAsync(restRequest).Result;
                bool hasNetWorkError = restResponse.ErrorException is System.Net.WebException;
                bool isReTrySuc      = !hasNetWorkError;
                return(isReTrySuc);
            }, remark: "HttpPost");

            //响应状态码是否表示成功 OK(200)
            if (!restResponse.IsSuccessful)
            {
                var errSb = new StringBuilder();
                errSb.AppendLine($"{url}接口异常");
                errSb.AppendLine($"Content:{restResponse.Content}");
                errSb.AppendLine($"StatusCode:{restResponse.StatusCode}");
                errSb.AppendLine($"ResponseStatus:{restResponse.ResponseStatus}");
                errSb.AppendLine($"ErrorMessage:{restResponse.ErrorMessage}");

                throw new Exception(errSb.ToString(), restResponse.ErrorException);
            }

            return(restResponse.Content);
        }
Esempio n. 3
0
        public static T HttpPost <T>(this string url, Dictionary <string, object> postParams, HttpPostCfg httpCfg = null)
        {
            string resultStr = url.HttpPost(postParams, httpCfg);

            var result = JsonConvert.DeserializeObject <T>(resultStr);

            return(result);
        }