C# 使用带有HttpWebRequest的TextWriter流时内存不足

C# 使用带有HttpWebRequest的TextWriter流时内存不足,c#,memory,httpwebrequest,textwriter,C#,Memory,Httpwebrequest,Textwriter,感谢您对OOM(内存不足)问题的关注,我在为web服务流式传输文件的代码中看到了这个问题。[我希望可以启动另一个提供更多细节的线程。]根据建议,我缩小了用于读取文件的缓冲区大小,看起来内存消耗更好,但我仍然看到OOM问题,并且我看到文件大小只有5MB时存在此问题。我可能想处理十倍大的文件 我现在的问题似乎是使用TextWriter 我创建了一个请求,如下所示[通过一些编辑来缩减代码]: HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Cr

感谢您对OOM(内存不足)问题的关注,我在为web服务流式传输文件的代码中看到了这个问题。[我希望可以启动另一个提供更多细节的线程。]根据建议,我缩小了用于读取文件的缓冲区大小,看起来内存消耗更好,但我仍然看到OOM问题,并且我看到文件大小只有5MB时存在此问题。我可能想处理十倍大的文件

我现在的问题似乎是使用TextWriter

我创建了一个请求,如下所示[通过一些编辑来缩减代码]:

HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(new Uri(strURL));
oRequest.Method = httpMethod;
oRequest.ContentType = "application/atom+xml";
oRequest.Headers["Authorization"] = getAuthHeader();
oRequest.ContentLength = strHead.Length + strTail.Length + longContentSize;
oRequest.SendChunked = true;

using (TextWriter tw = new StreamWriter(oRequest.GetRequestStream()))
{
    tw.Write(strHead);
    using (FileStream fileStream = new FileStream(strPath, FileMode.Open, 
           FileAccess.Read, System.IO.FileShare.ReadWrite))
    {
        StreamEncode(fileStream, tw);
    }
    tw.Write(strTail);
}
.....
这将调用到例程中:

public void StreamEncode(FileStream inputStream, TextWriter tw)
{
    // For Base64 there are 4 bytes output for every 3 bytes of input
    byte[] base64Block = new byte[9000];
    int bytesRead = 0;
    string base64String = null;

    do
    {
        // read one block from the input stream
        bytesRead = inputStream.Read(base64Block, 0, base64Block.Length);

        // encode the base64 string
        base64String = Convert.ToBase64String(base64Block, 0, bytesRead);

        // write the string
        tw.Write(base64String);


    } while (bytesRead !=0 );

}
由于潜在的大量内容,我是否应该使用TextWriter以外的东西?创建请求的整个有效负载似乎非常方便

这完全是错误的做法吗?我希望能够支持非常大的文件