日期:2014-05-18 浏览次数:20708 次
public static void DownloadToFile(string url, string saveName, CookieContainer cookie) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Credentials = CredentialCache.DefaultCredentials; if (cookie != null) request.CookieContainer = cookie; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream dataStream = response.GetResponseStream()) { using (FileStream fs = new FileStream(saveName, FileMode.Create, FileAccess.Write)) { byte[] buffer = new byte[1024]; while (true) { int sizeRead = dataStream.Read(buffer, 0, buffer.Length); if (sizeRead > 0) fs.Write(buffer, 0, sizeRead); else break; } fs.Flush(); } } }