how do i download a file (zip) from the internet in vb.net
experts. I'm trying to create a simple file downloader (with GUI). I'm pretty new to Vb.net and I didn't find any usefull information about this in the books I own. All tutorials in the wild are making me more frustrated, since the snippets I use, in most cases, are different classes. So I have some basic questions:
- What should I use?
HttpWebRequest/HttpWebResponse
orFileWebRequest/FileWebResponse
? - How do I save the
ResponseStream
to a file ? - Is it possible to use ResponseStream to define the kind of File (FileInfo) Class to get basic file information (size, extension, etc)
- is it generally neccessary, if
GetResponse()开发者_开发技巧.headers
gives you most of this info ?
- is it generally neccessary, if
My code, which sucks, because I do not know how to realize the saving....
Private Sub btn_downloader_Click(ByVal sender...) Handles btn_downloader.Click
'e.g http://codeigniter.com/download.php'
Dim fileUrl As String = txtBox_url.Text
Dim webUri = New Uri(fileUrl)
Dim wReq As HttpWebRequest = WebRequest.Create(webUri)
Dim wRes As HttpWebResponse = wReq.GetResponse()
Dim resUri As Uri = wRes.ResponseUri
'to ckeck if http://codeigniter.com/download.php gives http://codeigniter.com/download_files/reactor/CodeIgniter_2.0.0.zip and it does!'
Dim resStream As Stream = wRes.GetResponseStream()
And now I have to somehow use one of the Stream methods to write the file (at this stage only as a Stream), but do not know how. I saw this in the tutorials
resStream.Read(buffer, offset, count)
but I don't know what the buffer is and what it is for. Should I set some static values or any dynamic which deppends on Stream-data ?
Need help!
P.S. Sorry for bad English
If you are looking to do it quickly without a progress bar or anything that tracks download speed, estimated time left, etc. then I would suggest WebClient.DownloadFileAsync or WebClient.DownloadFile
. You should be able to read the mime type with WebClient.ResponseHeaders["content-type"]
and then rename the file accordingly.
This is not complete but may get you in right direction. 'Location is where to save file..
Public Function DownloadFile(ByVal URL As String, ByVal Location As String) As Boolean
Try
Dim WC As New WebClient
WC.DownloadFile(URL, Location)
Return True
Catch ex As Exception
Return False
End Try
End Function
精彩评论