Simple web File download in VB.NET
This is a simple program that shows how to download files from the web and save them.
This is a simple program that shows how to download files from the web
and save them. The program uses the HttpWebRequest and HttpWebResponse classes
to request and retrieve the requested file. The data are read into a buffer. A
FileStream class is used to save the file to disk. In this example, a doppler
radar map that covers the area I live is requested and saved to a file called
"weather.jpg". Since the data are downloaded and read into the buffer
asynchronously, a loop is required to read and keep track of how many bytes have
been read, and the point in the stream where the next read should start. The
loop will continue until the buffer is full or 0 bytes are read, indicating the
end of the stream has been reached. The buffer must be sized large enough to
hold the file. This is not a problem in this case as the doppler jpg's are a
standard and known size.
Requires .NET SDK
How To Compile?
vbc /r:System.Net.dll /r:System.IO.dll webretrieve.vb.
Shared Sub Main()
wr As HttpWebRequest =
Dim ws As
HttpWebResponse = CType(wr.GetResponse(),
Dim str As
Stream = ws.GetResponseStream()
Dim bytesToRead As Integer =
bytesToRead > 0
Dim n As Integer = str.Read(inBuf, bytesRead,
If n = 0
bytesRead += n
bytesToRead -= n
Dim fstr As New FileStream("weather.jpg",
fstr.Write(inBuf, 0, bytesRead)
End Sub 'Main