我如何在 vb.net 中从互联网下载文件 (zip)
专家。我正在尝试创建一个简单的文件下载器(带有 GUI)。我对 Vb.net 还很陌生,在我拥有的书中没有找到任何关于此的有用信息。所有野外教程都让我更加沮丧,因为在大多数情况下,我使用的片段是不同的类。 所以我有一些基本问题:
- 我应该使用什么?
HttpWebRequest/HttpWebResponse
或FileWebRequest/FileWebResponse
? - 如何将
ResponseStream
保存到文件中? - 是否可以使用ResponseStream定义类型的File(FileInfo)类来获取基本文件信息(大小、扩展名等)
- 如果
GetResponse().headers
能够为您提供最多的信息,那么这通常是必要的吗? 这些信息?
- 如果
我的代码很糟糕,因为我不知道如何实现保存......
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()
现在我必须以某种方式使用 Stream 方法之一来写入文件(现阶段仅作为 Stream),但不知道如何。 我在教程中看到了这一点
resStream.Read(buffer, offset, count)
,但我不知道缓冲区是什么以及它的用途。我应该设置一些静态值还是任何取决于 Stream-data 的动态值?
需要帮助!
PS抱歉英语不好
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望在没有进度条或任何跟踪下载速度、预计剩余时间等的情况下快速完成此操作,那么我建议您使用 WebClient.DownloadFileAsync 或 WebClient.DownloadFile。您应该能够使用
WebClient.ResponseHeaders["content-type"]
读取 mime 类型,然后相应地重命名该文件。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 withWebClient.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..