Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从HttpWebResponse流返回的字节数不等于ContentLength_C#_Http_Stream_Httpwebrequest_Httpwebresponse - Fatal编程技术网

C# 从HttpWebResponse流返回的字节数不等于ContentLength

C# 从HttpWebResponse流返回的字节数不等于ContentLength,c#,http,stream,httpwebrequest,httpwebresponse,C#,Http,Stream,Httpwebrequest,Httpwebresponse,我正在使用HttpWebRequest、HttpWebResponse和Stream创建下载管理器。首先,在从流开始读取文件内容之前,我从HttpWebResponse获取ContentLength,并在完成流时使用while循环停止读取。读取返回0,这意味着流结束,我注意到与ContentLength相比,我并没有收到所有字节 这是我用来从流中读取字节、计算内容长度到兆字节、接收到的字节数和接收到的字节百分比的一些代码 int maxReadSize = 102

我正在使用HttpWebRequest、HttpWebResponse和Stream创建下载管理器。首先,在从流开始读取文件内容之前,我从HttpWebResponse获取ContentLength,并在完成流时使用while循环停止读取。读取返回0,这意味着流结束,我注意到与ContentLength相比,我并没有收到所有字节

这是我用来从流中读取字节、计算内容长度到兆字节、接收到的字节数和接收到的字节百分比的一些代码

                int maxReadSize = 102400;
                int readByte = 0;
                long downloadedSize = 0;
                int roundCount = 0;
                int byteCalRound = 0;
                byte[] buffer = new byte[maxReadSize];
                _currentFile.Status = DownloadStatus.Downloading;
                DateTime lastUpdate = DateTime.Now;

                do
                {
                    readByte = await _inStream.ReadAsync(buffer, 0, maxReadSize, _ct);
                    byteCalRound += readByte;
                    downloadedSize += readByte;
                    roundCount++;

                    if (roundCount == 5)
                    {
                        var now = DateTime.Now;
                        var interval = (now - lastUpdate).TotalSeconds;
                        var speed = (int)Math.Floor(byteCalRound / interval);
                        lastUpdate = now;
                        _currentFile.RecievedSize = downloadedSize;
                        _currentFile.Throughput = speed;
                        _currentFile.PercentProgress = downloadedSize;

                        byteCalRound = 0;
                        roundCount = 0;
                    }

                    await stream.WriteAsync(buffer, 0, readByte);
                } while (readByte != 0);
            }

            // Download completed
            _currentFile.Status = DownloadStatus.Complete;
            _currentFile.CompleteDate = DateTime.Now;

            ...

            // Calculate file size
            public double FileSize
            {
                get { return _fileSize; }
                set
                {
                    _fileSize = value / 1048576;
                }
            }

            // Calculate receive bytes
            public double RecievedSize
            {
                get { return _recievedSize; }
                set
                {
                    _recievedSize = value / 1048576;
                }
            }

            // Calculate percent
            public double PercentProgress
            {
                get { return _percentProgress; }
                set
                {
                    _percentProgress = value / 1048576 * 100 / _fileSize;
                }
            }
结果我下载并测试的字节数有时是我从PercentProgress和ReceivedSize检查的所有字节数 有时我再次收到99.6-99.9%的文件,我检查了PercentProgress和ReceivedSize,这就是我面临的问题

那么,问题是什么导致了这个问题


请注意,我一直只在一个网站上下载视频进行测试,因为我不认为这个问题只发生在我通常通过Chrome下载的这个网站上,结果是我收到了100%的文件

您正在读取的数字PercentProgress和ReceivedSize仅在循环的每5次迭代中更新一次。如果有12次迭代,它们将反映前10次迭代的大小

您可以在以后最后一次更新它们,即:

        ...
    } while (readByte != 0);
}

// Download completed
_currentFile.Status = DownloadStatus.Complete;
_currentFile.CompleteDate = DateTime.Now;
_currentFile.RecievedSize = downloadedSize;
_currentFile.PercentProgress = downloadedSize;

另外,如果您在团队中工作,请记住,大多数人都希望属性的get方法返回上次设置的值

谢谢,我以前没注意到XD