• netcore HttpClient Post 提交数据


    这里列出了两种选择 HttpClient 异步提交

    1.HttpClient:

      public static string HttpClientPost(string url,string requestJson)
            {
                try
                {
                    string result = string.Empty;
                    Uri postUrl = new Uri(url);
    
                    using (HttpContent httpContent = new StringContent(requestJson))
                    {
                        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                     
                        using (var httpClient = new HttpClient())
                        {
                            httpClient.Timeout = new TimeSpan(0, 0, 60);
                            result = httpClient.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result;
                            
                        }
    
                    }
                    return result; 
                }
                catch (Exception e)
                { 
                    throw e;
                } 
            }

    对应接收数据:

        [Route("api/[controller]/[action]")]
        [ApiController]
        [Authorize]
       
        public class WareHouseController : ControllerBase
        { 
    
       [HttpPost] 
            [AllowAnonymous]
            public IActionResult Test([FromBody]ModifyModel model)
            { try
                {
                     
                }
                catch (Exception e)
                { 
                }
                return new JsonResult( );
            }
    }

    第二种请求方式:

                    //var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                    ////请求
                    //HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
                    //if (response.IsSuccessStatusCode)
                    //{
                    //    Task<string> t = response.Content.ReadAsStringAsync();
                    //    if (t != null)
                    //    {
                    //        return t.Result;
                    //    }
                    //}
                    //return string.Empty;
  • 相关阅读:
    env文件的作用
    Ubuntu 卸载wine
    Linux配置yaf3.x.x环境
    yaf中使用Cli模式,也就是定时器
    Yaf 在Bootstrap中注册变量,然后在其他地方进行使用!
    yaf 查看配置
    yaf配置通用函数
    一个严谨的接口调用
    后台基础表
    tensorflow环境搭建
  • 原文地址:https://www.cnblogs.com/wfpanskxin/p/12849231.html
Copyright © 2020-2023  润新知