C# 后端post请求帮助类

/// <summary>
        /// post请求类
        /// </summary>
        /// <param name="Url">请求地址</param>
        /// <param name="Token">Token为空表示没有</param>
        /// <param name="Parameter">参数</param>
        /// <returns></returns>
        public string sendHttpPost(string Url, string Token, object Parameter)
        {
            var result = "";
            try
            {
                var client = new RestClient(Url);
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                if (!string.IsNullOrEmpty(Token))
                {
                    request.AddHeader("token", Token);
                }

                request.AddParameter("application/json", JsonConvert.SerializeObject(Parameter), ParameterType.RequestBody);
                result = client.Execute(request).Content;

            }
            catch (Exception e)
            {
                LogHelper.Error("POST数据出错", e);

            }
            return result;
        }

 

posted @ 2022-01-06 13:56  Provedl  阅读(257)  评论(0编辑  收藏  举报