zoukankan      html  css  js  c++  java
  • .net core web api 与httpclient发送和接收文件及数据

    客户端 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 

            [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 };
            }
  • 相关阅读:
    Redis常见7种使用场景(PHP)
    阻塞式I/O实现简单TCP通信
    telnet客户端程序
    TCP简单回射程序
    getsockname和getpeername函数
    close函数
    TCP时间获取程序
    listen函数
    基本套接字编程
    readline.c
  • 原文地址:https://www.cnblogs.com/dayang12525/p/10688842.html
Copyright © 2011-2022 走看看