从 Web 客户端流读取时缺少字节

本文关键字:字节 读取 Web 客户端 | 更新日期: 2023-09-27 18:34:43

为什么从WebClient流中读取时丢失字节,如下所示?

const int chuckDim = 80;
System.Net.WebClient client = new System.Net.WebClient();
Stream stream = client.OpenRead("http://media-cdn.tripadvisor.com/media/photo-s/01/70/3e/a9/needed-backup-lol.jpg");
//Stream stream = client.OpenRead("file:///C:/Users/Tanganello/Downloads/needed-backup-lol.jpg");
//searching file length
WebHeaderCollection whc = client.ResponseHeaders;
int totalLength = (Int32.Parse(whc["Content-Length"]));
byte[] buffer = new byte[totalLength];
//reading and writing
FileStream filestream = new FileStream("C:''Users''Tanganello''Downloads''clone1.jpg", FileMode.Create, FileAccess.ReadWrite);
int accumulator = 0;
while (accumulator + chuckDim < totalLength) {
    stream.Read(buffer, accumulator, chuckDim);
    filestream.Write(buffer, accumulator, chuckDim);
    accumulator += chuckDim;
}
stream.Read(buffer, accumulator, totalLength - accumulator);
filestream.Write(buffer, accumulator, totalLength - accumulator);
stream.Close();
filestream.Flush();
filestream.Close();

这是我从第一个流中得到的:http://img839.imageshack.us/img839/830/clone1h.jpg

从 Web 客户端流读取时缺少字节

问题是你忽略了 Stream.Read 方法的返回值:

计数

要从当前流读取的最大字节数。

返回值

读入缓冲区的总字节数。这可以小于请求的字节数

<小时 />

您可以通过简单地使用 WebClient.DownloadFile 方法来避免读取和写入流的整个业务:

using (var client = new WebClient())
{
    client.DownloadFile(
        "http://media-cdn.tripadvisor.com/media/photo-s/01/70/3e/a9/needed-backup-lol.jpg",
        "C:''Users''Tanganello''Downloads''clone1.jpg");
}

或者,如果你真的想使用流,你可以简单地使用 Stream.CopyTo 方法:

using (var client = new WebClient())
using (var stream = client.OpenRead("http://..."))
using (var file = File.OpenWrite("C:''..."))
{
    stream.CopyTo(file);
}

如果您坚持自己真正复制字节,则正确的方法如下:

using (var client = new WebClient())
using (var stream = client.OpenRead("http://..."))
using (var file = File.OpenWrite("C:''..."))
{
    var buffer = new byte[512];
    int bytesReceived;
    while ((bytesReceived = stream.Read(buffer, 0, buffer.Length)) != 0)
    {
        file.Write(buffer, 0, bytesReceived);
    }
}