如果我从 WebRequest.GetResponseStream 的流中读取,我是从内存中读取还是从网络中读取
If I read from a stream from WebRequest.GetResponseStream am I reading from memory or the network
我有以下从 FTP 服务器下载文件的代码:
var req = (FtpWebRequest)WebRequest.Create(ftp_addr + file_path);
req.Credentials = new NetworkCredential(...);
req.Method = WebRequestMethods.Ftp.DownloadFile;
using (var resp = req.GetResponse())
using (var strm = resp.GetResponseStream())
using (var mem_strm = new MemoryStream())
{
//The Web Response stream doesn't support seek, so we have to do a buffered read into a memory stream
byte[] buffer = new byte[2048];
int red_byts = 0;
do
{
red_byts = strm.Read(buffer, 0, 2048);
mem_strm.Write(buffer, 0, red_byts);
}
while (strm.CanRead && red_byts > 0);
//Reset the stream to position 0 for reading
mem_strm.Position = 0;
//Now I've got a mem stream that I can use
}
因为无法读取或查找从 "GetResponseStream" 返回的原始流(无法对其执行查找)。在我看来,这段代码实际上是在向 FTP 服务器请求下一个文件块并将其复制到内存中。我说得对吗?还是在您可以 GetResponseStream 时下载了整个响应?
我只是想知道,这样我就可以在使用 FTP 下载的异步方法中正确地应用等待和 ReadAsync 调用。我的直觉告诉我改行:
red_byts = strm.Read(...);
到
red_byts = await strm.ReadAsync(...);
WebRequest.GetResponseStream 的文档似乎没有指定,FtpWebRequest.GetResponseStream 的文档也没有指定。
原始流应该是网络流;要验证这一点,请查看 strm.GetType().Name
.
It seems to me that this code is actually performing a request to the FTP server for the next chunk of the file and copying it into memory. Am I correct, or is the entire response downloaded when you can GetResponseStream?
都没有。
不是为每个调用发送单独的请求到Read
/ReadAsync
;相反,只有一个请求,流表示一个响应的主体。
也是不是在从GetResponseStream
返回之前下载整个响应。相反,流表示下载。 一些 缓冲正在进行 - 当服务器正在发送数据时,网络堆栈和 BCL 正在为您读取它 - 但不能保证它在您 开始读取流。
I just want to know so I can correctly apply awaits with ReadAsync calls in asynchronous methods that make use of FTP downloading. My intuition tells me to [use async]
是的,您应该使用异步读取。如果一些数据已经被缓冲,它们可能会同步完成;否则,他们将需要等到服务器发送更多数据。
我有以下从 FTP 服务器下载文件的代码:
var req = (FtpWebRequest)WebRequest.Create(ftp_addr + file_path);
req.Credentials = new NetworkCredential(...);
req.Method = WebRequestMethods.Ftp.DownloadFile;
using (var resp = req.GetResponse())
using (var strm = resp.GetResponseStream())
using (var mem_strm = new MemoryStream())
{
//The Web Response stream doesn't support seek, so we have to do a buffered read into a memory stream
byte[] buffer = new byte[2048];
int red_byts = 0;
do
{
red_byts = strm.Read(buffer, 0, 2048);
mem_strm.Write(buffer, 0, red_byts);
}
while (strm.CanRead && red_byts > 0);
//Reset the stream to position 0 for reading
mem_strm.Position = 0;
//Now I've got a mem stream that I can use
}
因为无法读取或查找从 "GetResponseStream" 返回的原始流(无法对其执行查找)。在我看来,这段代码实际上是在向 FTP 服务器请求下一个文件块并将其复制到内存中。我说得对吗?还是在您可以 GetResponseStream 时下载了整个响应?
我只是想知道,这样我就可以在使用 FTP 下载的异步方法中正确地应用等待和 ReadAsync 调用。我的直觉告诉我改行:
red_byts = strm.Read(...);
到
red_byts = await strm.ReadAsync(...);
WebRequest.GetResponseStream 的文档似乎没有指定,FtpWebRequest.GetResponseStream 的文档也没有指定。
原始流应该是网络流;要验证这一点,请查看 strm.GetType().Name
.
It seems to me that this code is actually performing a request to the FTP server for the next chunk of the file and copying it into memory. Am I correct, or is the entire response downloaded when you can GetResponseStream?
都没有。
不是为每个调用发送单独的请求到Read
/ReadAsync
;相反,只有一个请求,流表示一个响应的主体。
也是不是在从GetResponseStream
返回之前下载整个响应。相反,流表示下载。 一些 缓冲正在进行 - 当服务器正在发送数据时,网络堆栈和 BCL 正在为您读取它 - 但不能保证它在您 开始读取流。
I just want to know so I can correctly apply awaits with ReadAsync calls in asynchronous methods that make use of FTP downloading. My intuition tells me to [use async]
是的,您应该使用异步读取。如果一些数据已经被缓冲,它们可能会同步完成;否则,他们将需要等到服务器发送更多数据。