獲取網路文件,通過流保存文件。 //網路路徑文件 string pathUrl = "http://localhost:805/js/site.zip"; System.Net.HttpWebRequest request = null; System.Net.HttpWebResponse res ...
獲取網路文件,通過流保存文件。
//網路路徑文件
string pathUrl = "http://localhost:805/js/site.zip";
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
//請求網路路徑地址
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(pathUrl);
request.Timeout = 5000; // 超時時間
//獲得請求結果
response = (System.Net.HttpWebResponse)request.GetResponse();
//文件下載地址
string path = "/文件下載";
// 如果不存在就創建file文件夾
if (!Directory.Exists(path))
{
if (path != null) Directory.CreateDirectory(path);
}
path = path + "/site.zip";
//長度
byte[] img = new byte[response.ContentLength];
Stream i = response.GetResponseStream();
//讀取二進位流
i.Read(img, 0, Convert.ToInt32(response.ContentLength));//這裡獲取之後
i.Close();
i.Dispose();
//保存為指定文件
File.WriteAllBytes(path, img);
response.Close();