. net HTTP POST FileStream /不使用内存流

本文关键字:内存 HTTP POST FileStream net | 更新日期: 2023-09-27 18:06:56

HttpClient.PostAsync(uri, StreamContent)似乎在幕后使用MemoryStream,即使我使用FileStream创建StreamContent。有没有办法从。net HTTP POST FileStream不使用MemoryStream ?我可以修改MS.Internal.InternalWebRequestStream.Write(参见下面的堆栈跟踪)以不使用MemoryStream吗?

堆栈跟踪:

Exception of type 'System.OutOfMemoryException' was thrown. 
   at System.IO.MemoryStream.set_Capacity(Int32 value) 
   at System.IO.MemoryStream.EnsureCapacity(Int32 value) 
   at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) 
   at MS.Internal.InternalWebRequestStream.Write(Byte[] buffer, Int32 offset, Int32 count) 
   at System.IO.Stream.<BeginWriteInternal>b__11(Object param0) 
   at System.Threading.Tasks.Task`1.InnerInvoke() 
   at System.Threading.Tasks.Task.Execute() 

. net HTTP POST FileStream /不使用内存流

解决方案是使用HttpWebRequest并按照这里的描述设置AllowWriteStreamBuffering = false