我在我本机上搭建HTTP网站,做了下面这个实验,结果很不理解,求各位帮忙给个说法。Stream.Read() 长度是根据什么来定,如本例中问什么是3867?GetResponseStream()返回的NetStream没有Length属性,但是不报错,为什么?
FileStream _fsLocal = new FileStream(@"D:\HTTP\About.jpg", FileMode.Open);
byte[] _bytesLoacal = new byte[_fsLocal.Length];
_fsLocal.Read(_bytesLoacal, 0,Convert.ToInt32(_fsLocal.Length));
string _path = http://XXX.XXX.X.XXX/About.jpg;
HttpWebRequest _hwRequest = (HttpWebRequest)WebRequest.Create(_path);
HttpWebResponse _hwResponse = (HttpWebResponse)_hwRequest.GetResponse ();
Stream _fileStream = _hwResponse.GetResponseStream();
Int32 _length = Convert.ToInt32(_hwResponse.ContentLength);
byte[] bytes = new byte[_length];
_fileStream.Read(bytes, 0, _length);
_fileStream.Close();
_hwResponse.Close();
for (int i = 0; i < _length; i++)
{
if (_bytesLoacal[i].ToString() != bytes[i].ToString())
{
MessageBox.Show(i.ToString()+"****"+_bytesLoacal[i].ToString()+"****"+bytes[i].ToString()+"\t\n");
}
}
FileStream _writeFile = new FileStream("D:\\About.jpg", FileMode.Create,FileAccess.Write);
_writeFile.Write(bytes, 0, _length);
MessageBox.Show("下载完成");
你要下载文件如果没什么特殊要求不如直接用WebClient.DownloadData更方便.