【问题标题】:Error while downloading a zip file from a website从网站下载 zip 文件时出错
【发布时间】:2011-03-26 22:28:58
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

  • 您是否使用实际 URL 而不是 "SourceURL"?您设置所有这些手动标题是否有原因?
  • 您的 URL 格式似乎是问题所在。尝试直接将Url设置为合适的地址; Dim Url as String = "http://someSite/directory/file.zip" 的形式
  • 是的,我正在使用正确的源 URL 而不是代码中的演示 URL。我已分配这些手动标头来假装来自 Web 浏览器的请求,否则我会从网站收到 403 错误。现在 403 错误已经解决了,但仍然没有有效的进展。

标签: vb.net zipfile


【解决方案1】:

您的_WebClient.Headers("Accept") = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 表示您只准备接受 html 或 xml 文档...您可能希望将其更改为接受 zip 文件“application/zip, application/octet-stream”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    相关资源
    最近更新 更多