首页 > 解决方案 > Httpclient.SendAsync loads StreamContent into memory before sending it to the server

问题描述

I'm trying to perform a large video upload to a server, but no matter what I do, if I'm using Httpclient.SendAsync(HttpResponseMessage) to send the content I have assigned to the HttpResponseMessage among with all the headers and such, the whole content will be loaded into the memory.

But I'm using HttpClient.PostAsync(uri, HttpResponseMessage.Content), it'll do the upload normally, without loading the content into the stream.

Is there any workaround, rather than just switch to PostAsync? It doesn't offer me the possibilities SendAsync offers, so I'd rather not do.

Example:

{
    var multiForm = new MultipartFormDataContent(UploadId);
    var fileStream = File.OpenRead(videoPath);
    var streamContent = new StreamContent(fileStream);
    multiForm.Add(streamContent, "video", Path.GetFileName(fs.Name));

    var request = GetHttpReqeustMessage[Method.Post, uri];
    request.Content = multiForm;

    var result = client.SendAsync(request);
}

标签: c#.netvb.nethttpclientsendasync

解决方案


I'm so dumb. I forgot I was tracking the logs for every httprequestmessage, so I'd print everything within a request, including the bytes of the whole video. I want to fly, far away.


推荐阅读