Net Core教程

C# 后端post请求帮助类

本文主要是介绍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;
        }

 

这篇关于C# 后端post请求帮助类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!