.net core web api 与httpclient发送和接收文件及数据【转载】

转:https://www.cnblogs.com/dayang12525/p/10688842.html

客户端 HttpClient

.net core web api 与httpclient发送和接收文件及数据【转载】
         var url = $"https://localhost:44323/api/values/posttest?resource_source=yangwwmessage";
                using (HttpClient _client = new HttpClient())
                {
                    using (var multiContent = new MultipartFormDataContent())
                    {
                        var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(imgPath));
                        multiContent.Add(fileContent, "file", Path.GetFileName(imgPath));

                        HttpResponseMessage response = _client.PostAsync(url, multiContent).Result;
                        string data = response.Content.ReadAsStringAsync().Result;
                    }
                }
.net core web api 与httpclient发送和接收文件及数据【转载】

服务端.net core Web Api 

.net core web api 与httpclient发送和接收文件及数据【转载】
        [HttpPost]
        [Route("PostTest")]
        public JsonResult PostTest()
        {
            var r = Request.Query["resource_source"].ToString();
            var files = HttpContext.Request.Form.Files;
            return new JsonResult("this is a test api for me") { StatusCode = 200 };
        }
.net core web api 与httpclient发送和接收文件及数据【转载】
上一篇:Apache HttpClient工具类


下一篇:【Java】http client 通过FD耗尽实验谈谈使用HttpClient的正确姿势