我们可以用 web API 发送一个内存流对象吗?

Can we send a memory stream object with web API?

我的服务器端有一个内存流对象,这个对象应该可以在另一方访问,我称它为我的 API 的客户端或消费者。 在服务器端我有这样的方法(parameters.Save与第三方库有关)

    public MemoryStream GetSerializedParameters()
    {
        var parameters = GetParameters();
        MemoryStream memory = new MemoryStream();
        parameters.Save(memory);
        return memory;
    }

我正在考虑将此内存流发送到具有网络的客户端 API,所以我的操作是这样的:

[HttpGet("parameters")]
    public HttpResponseMessage GetParameters()
    {
        var stream = _server.GetSerializedParameters();
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentLength = stream.Length;
        return result;
    }

我不确定这是否是正确的方式,这个实现是否正确,因为我无法使用它: 我不知道我必须使用哪种 httpClient 方法:ReadAsStreamAsync() 或其他任何方法,因为我找不到任何工作

当然可以:

 [HttpGet]
        public async Task Get()
        {
            var randomString = "thisIsCool";
            var randomStringBytes = Encoding.UTF8.GetBytes(randomString);
            using (var ms = new MemoryStream(randomStringBytes))
            {
                await ms.CopyToAsync(this.Response.Body);
            }
        

根据我的理解,下面的代码可能对您有所帮助:

网页版API:

        [HttpGet]
    public HttpResponseMessage ReadToStream(HttpRequestMessage requestMessage)
    {
       
            var streamObj = _server.GetSerializedParameters();
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new StreamContent(streamObj);
            requestMessage.RegisterForDispose(streamObj);
            response.StatusCode = HttpStatusCode.OK;

            return response;
   }

客户端

public async Task<string> DownloadFile(string guid)
{
var fileInfo = new FileInfo($"{guid}.txt");

var response = await _httpClient.GetAsync($"{url}/api/fileDownloadAPI?guid={guid}");
response.EnsureSuccessStatusCode();
await using var ms = await response.Content.ReadAsStreamAsync();
await using var fs = File.Create(fileInfo.FullName);
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(fs);
return fileInfo.FullName;
}

我找到了这样的解决方案: 这是在服务器端:

    [HttpGet("parameters")]
    public IActionResult GetParameters()
    {
        var stream = _server.GetSerializedParameters();
        stream.Seek(0, SeekOrigin.Begin);
        return File(stream, MediaTypeNames.Text.Plain, "parameters.txt");
    }

这里是 client-side:

        public MemoryStream StoreParameters()
    {
        var request =new HttpRequestMessage
        {
            RequestUri = new Uri("https://localhost:44316/api/parameters"),
            Method = HttpMethod.Get
        };
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var result = _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result;
        var ms = new MemoryStream();

        result.Content.CopyToAsync(ms).Wait();
        return result.IsSuccessStatusCode ? ms: null;
    }