从网站下载 zip 文件时出错

发布于 2024-10-27 02:25:51 字数 1778 浏览 3 评论 0原文

我正在尝试使用 Web 客户端从网站下载 zip 文件,但该文件未正确下载。

每次下载 zip 文件时,目标目录中都没有内容。

下面我粘贴我正在使用的代码:

Imports System.Net
Imports System.IO
Imports System.Collections.Generic
Imports System.Text
Imports System.Net.NetworkInformation
Imports System.Diagnostics


 Public Class Form1
Private Sub _DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    ' Update progress bar
    ProgressBar1.Value = e.ProgressPercentage
End Sub

Private Sub _DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    ' File download completed
    Button1.Enabled = Enabled
    MessageBox.Show("Download completed")
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Enabled = False
    Dim ProcessingDate As Date = DateTime.Now
    Dim File2Download As String = "File Name To Be Downloaded.zip"
    Dim Url As String = "SourceURL" & File2Download 'http site
    Dim _WebClient As New System.Net.WebClient
    _WebClient.UseDefaultCredentials = True
    _WebClient.Headers("User-Agent") = "Mozilla/5.0 (compatible; MSIE 9.0; windows NT 6.1; WOW64; Trident/5.0)"
    _WebClient.Headers("Method") = "GET"
    _WebClient.Headers("AllowAutoRedirect") = True
    _WebClient.Headers("KeepAlive") = True
    _WebClient.Headers("Accept") = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    AddHandler _WebClient.DownloadFileCompleted, AddressOf _DownloadFileCompleted
    AddHandler _WebClient.DownloadProgressChanged, AddressOf _DownloadProgressChanged
    _WebClient.DownloadFileAsync(New Uri(Url), "Destination Directory\" & File2Download)
End Sub
End Class

恳请在这方面提供任何帮助。

I am trying to download a zip file from a website by using a web client but the file is not getting downloaded properly.

Every time the zip file is getting downloaded with no contents in it,in the destination directory.

Below I paste the code I am using :

Imports System.Net
Imports System.IO
Imports System.Collections.Generic
Imports System.Text
Imports System.Net.NetworkInformation
Imports System.Diagnostics


 Public Class Form1
Private Sub _DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    ' Update progress bar
    ProgressBar1.Value = e.ProgressPercentage
End Sub

Private Sub _DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    ' File download completed
    Button1.Enabled = Enabled
    MessageBox.Show("Download completed")
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Enabled = False
    Dim ProcessingDate As Date = DateTime.Now
    Dim File2Download As String = "File Name To Be Downloaded.zip"
    Dim Url As String = "SourceURL" & File2Download 'http site
    Dim _WebClient As New System.Net.WebClient
    _WebClient.UseDefaultCredentials = True
    _WebClient.Headers("User-Agent") = "Mozilla/5.0 (compatible; MSIE 9.0; windows NT 6.1; WOW64; Trident/5.0)"
    _WebClient.Headers("Method") = "GET"
    _WebClient.Headers("AllowAutoRedirect") = True
    _WebClient.Headers("KeepAlive") = True
    _WebClient.Headers("Accept") = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    AddHandler _WebClient.DownloadFileCompleted, AddressOf _DownloadFileCompleted
    AddHandler _WebClient.DownloadProgressChanged, AddressOf _DownloadProgressChanged
    _WebClient.DownloadFileAsync(New Uri(Url), "Destination Directory\" & File2Download)
End Sub
End Class

Any help in this regard is earnestly solicited.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小镇女孩 2024-11-03 02:25:51

您的 _WebClient.Headers("Accept") = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 意味着您只是准备好了接受 html 或 xml 文档...您可能需要更改它以接受 zip 文件“application/zip, application/octet-stream”

Your _WebClient.Headers("Accept") = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" implies that you are only prepared to accept html or xml documents... you might want to change that to accept zip files "application/zip, application/octet-stream"

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