HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://files.cnblogs.com/files/shousiji/ProcTools.rar");
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream fileStream = response.GetResponseStream();
int fileContentLength = (int)response.ContentLength;
byte[] fileBytes = new byte[fileContentLength];
fileStream.Read(fileBytes, 0, fileContentLength);
using (FileStream stream = File.Create("c:/2.rar"))
{
stream.Write(fileBytes, 0, fileBytes.Length);
stream.Close();
}
代码保存的文件,大小是一样了,但打开是已损坏的。
不知道怎么做了,各位大神帮忙看看!
试试下面的代码
using (var response = request.GetResponse() as HttpWebResponse)
{
using (var responseStream = response.GetResponseStream())
{
using (var fs = new FileStream("c:/2.rar", FileMode.Create))
{
responseStream.CopyTo(fs);
}
}
}
成功了!代码还简单了,太厉害了,感谢感谢!