c# Http下載文件
/// <summary>
/// Http下載文件
/// </summary>
public void HttpDownloadFile(string url, string path)
{
// 設置參數
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//發送請求並獲取相應回應數據
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才開始向目標網頁發送Post請求
Stream responseStream = response.GetResponseStream();
//創建本地文件寫入流
Stream stream = new FileStream(path, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();
}
c# Http下載文件