我想從網上下載數千個文件並保存到本地。什麼是最有效的方法?失敗在10秒內超時非常重要。如何用vb .net下載超時下載文件?
有沒有更好的方法將一個流串入另一個流?也許一個較小的緩衝區,如一次1024個字節,對大文件更有效率?
Dim w_req As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create("http://blah.blah.blah/blah.html"), System.Net.HttpWebRequest)
w_req.Timeout = 10000
Dim w_res As System.Net.HttpWebResponse = CType(w_req.GetResponse(), System.Net.HttpWebResponse)
Dim br As New System.IO.BinaryReader(w_res.GetResponseStream())
Dim fs As New System.IO.FileStream(LocalFileName, IO.FileMode.CreateNew, IO.FileAccess.Write, IO.FileShare.Write)
Dim b() As Byte = br.ReadBytes(CInt(br.BaseStream.Length))
fs.Write(b, 0, b.Length)
fs.Close()
br.Close()
爲什麼要使用流時,你可以很容易地使用WebClient.DownloadFile? – svick 2010-02-04 14:34:17