我正在尝试读取 500 MB 的文本文件以通过 HttpWebRequest 发送它的内容。根据我的要求,我不能分块发送数据。代码如下:
using (StreamReader reader = new StreamReader(filename))
{
postData = reader.ReadToEnd();
}
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "text/plain";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(dataStream))
{
responseFromServer = reader.ReadToEnd();
}
Console.WriteLine(responseFromServer);
dataStream.Close();
response.Close();
读取这么大的文件会给我带来内存不足的异常。有没有办法我可以做到这一点?