简体   繁体   English

是否可以在没有HttpClient缓冲整个请求的情况下将PushStreamContent添加到MultipartFormDataContent?

[英]Can I add PushStreamContent to MultipartFormDataContent without HttpClient buffering whole request?

I am trying to send a large file in an HTTP POST, along with some other data, using a MultipartFormDataContent . 我正在尝试使用MultipartFormDataContent在HTTP POST中发送一个大文件以及其他一些数据。 To send the file in chunks, I am using a PushStreamContent which writes chunks of data from my file stream to output stream, flushing after each write: 为了分块发送文件,我使用的是PushStreamContent ,它将文件流中的数据块写入到输出流中,每次写入后都会刷新:

PushStreamContent pushStreamContent = new PushStreamContent((stream, content, context) => {
    byte[] buffer = new byte[4096];
    int bytesRead;
    do {
        bytesRead = fileStream.Read(buffer, 0, 4096);
        stream.Write(buffer, 0, bytesRead);
        stream.Flush();
    }
    while(bytesRead != 0);
    stream.Close();
});

If I POST this HttpContent using the following code, everything works fine: 如果我使用以下代码发布此HttpContent ,则一切正常:

new HttpClient().PostAsync(destinationUrl, pushStreamContent);

If, however, this PushStreamContent is added to a MultipartFormDataContent , as follows: 但是,如果将此PushStreamContent添加到MultipartFormDataContent ,则如下所示:

MultipartFormDataContent postForm = new MultipartFormDataContent {
    {stringContent, "atom"},
    {pushStreamContent, "binary"}
};

and that is posted, then I receive an OutOfMemoryException within the lambda of the PushStreamContent . 被贴出来,然后我收到OutOfMemoryException的的拉姆达内PushStreamContent Presumably, the HttpClient is buffering the whole of the MultipartFormDataContent into memory, which fails as the request content is very large. 据推测, HttpClient正在将整个 MultipartFormDataContent缓冲到内存中,由于请求内容非常大,该操作将失败。 I would like the client to defer to the flushing of the contained PushStreamContent . 我希望客户端PushStreamContent所包含的PushStreamContent Is this possible? 这可能吗? I am using .NET 4.5. 我正在使用.NET 4.5。

The request is being buffered because HttpClient needs to know the total length of the request in order to set the Content-Length header. 由于HttpClient需要知道请求的总长度才能设置Content-Length标头,因此正在缓冲该请求。 A PushStreamContent object always returns false for TryComputeLength(out long) . PushStreamContent对象对于TryComputeLength(out long) 始终返回false

Thus the solution is to create a sub-class of PushStreamContent which does know its length: 因此,解决方案是创建一个知道其长度的PushStreamContent子类:

class PushStreamContentWithLength : PushStreamContent {
    public PushStreamContentWithLength(Action<Stream, HttpContent, TransportContext> onStreamAvailable, long streamLength) : base(onStreamAvailable) {
        mStreamLength = streamLength;
    }

    readonly long mStreamLength;

    protected override bool TryComputeLength(out long length) {
        length = mStreamLength;
        return true;
    }
}

If one of these is added to a MultipartFormDataContent , the data is pushed to the stream during an HTTP post, rather than being pre-loaded into memory. 如果将其中之一添加到MultipartFormDataContent ,则在HTTP发布期间将数据推送到流,而不是预先加载到内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpClient和PushStreamContent - HttpClient and PushStreamContent 请求需要缓冲数据才能成功 HttpClient - The request requires buffering data to succeed HttpClient 使用PushStreamContent从HTTPClient上传 - Using PushStreamContent to upload from an HTTPClient 带参数的 HttpClient MultipartFormDataContent (C#) - HttpClient MultipartFormDataContent with parameters (C#) 如何使用MultipartFormDataContent为HttpClient请求设置Content-Type标头? - How do you set the Content-Type header for an HttpClient request with MultipartFormDataContent? HttpClient和MultipartFormDataContent =上传文件字符串 - HttpClient and MultipartFormDataContent = upload File an Strings 如何在不缓冲的情况下从WCF流式传输响应? - How can I stream a response from WCF without buffering? 我想添加从用户那里获得的用户 ID。 但我不知道如何使用 request.UserID 字段添加 multipartformdatacontent() - I want to add the userid I have obtained from the user. But I don't know how to add multipartformdatacontent () with request.UserID field 使用 HttpClient 中的 MultipartFormDataContent 发布数据 - Post data using MultipartFormDataContent from HttpClient 使用 HttpClient 将大文件流传递给 MultipartFormDataContent - Passing a large file stream to MultipartFormDataContent with HttpClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM