我如何在 vb.net 中从互联网下载文件 (zip)

发布于 2024-10-18 13:01:37 字数 1302 浏览 4 评论 0原文

专家。我正在尝试创建一个简单的文件下载器(带有 GUI)。我对 Vb.net 还很陌生,在我拥有的书中没有找到任何关于此的有用信息。所有野外教程都让我更加沮丧,因为在大多数情况下,我使用的片段是不同的类。 所以我有一些基本问题:

  • 我应该使用什么? HttpWebRequest/HttpWebResponseFileWebRequest/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 or
    FileWebRequest/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 ?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2024-10-25 13:01:37

如果您希望在没有进度条或任何跟踪下载速度、预计剩余时间等的情况下快速完成此操作,那么我建议您使用 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 with WebClient.ResponseHeaders["content-type"] and then rename the file accordingly.

七禾 2024-10-25 13:01:37

这并不完整,但可能会让您朝着正确的方向前进。
'位置是保存文件的位置..

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文