【问题标题】:.NET WebClient .DownloadFileAsync and .DownloadFile do nothing and do not throw exception.NET WebClient .DownloadFileAsync 和 .DownloadFile 什么都不做,也不抛出异常
【发布时间】:2021-09-12 16:38:03
【问题描述】:

第一次发帖。

我无法让 DownloadFileAsync 执行任何操作或生成异常。此外,普通的 jane DownloadFile 会生成一个本地文件,但它是空的。我怀疑问题出在提供响应的网站上,但 WebClient 似乎完全没有注意到什么都没有发生,这真的很烦人......

源文件在这里: https://www.sec.gov/Archives/edgar/daily-index/xbrl/companyfacts.zip

这是一个相当大的文件~800MB。如果我将 url 放到浏览器中,它会直接进入下载并在大约 5 分钟内完成。

使用 .NET Framework 4.7.2 运行 Win10。

你能提供的任何见解都会很棒。

'Already consulted all these releated posts: 
'https://stackoverflow.com/questions/39292147/how-can-i-download-a-zip-file-from-a-url-using-c
'https://stackoverflow.com/questions/54497601/how-do-i-show-progress-bar-when-downloading-a-file
'https://stackoverflow.com/questions/153451/how-to-check-if-system-net-webclient-downloaddata-Is-downloading-a-binary-file
'https://stackoverflow.com/questions/3272067/webclient-403-forbidden?rq=1
 
Public DownloadClient As WebClient

Public Sub DownloadAsync()
   ' this is an approximately 800MB file 
    Dim ArchiveUrl As String = "https://www.sec.gov/Archives/edgar/daily-index/xbrl/companyfacts.zip"
    Dim UserFolder As string = System.IO.Path.GetTempPath()
    Dim ArchiveTemp as String = Now().ToString("yy-MM-dd_HH-mm") & ".zip"

    If DownloadClient Is Nothing = False AndAlso DownloadClient.IsBusy Then
        Debug.WriteLine("File download in progress")
        Return
    End If

    Path = New FileInfo(System.IO.Path.Combine(UserFolder, ArchiveTemp))
    Debug.Print("Downloading to: " & Path.FullName)

    'For .net 4.
    'ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)

    'For .net4.5 Or later
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

    DownloadClient = New WebClient()
    'DownloadClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)")
    'DownloadClient.Headers.Add("user-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0")
    DownloadClient.Headers.Add("User-Agent: Other")

    AddHandler DownloadClient.DownloadDataCompleted, AddressOf DownloadComplete
    AddHandler DownloadClient.DownloadProgressChanged, AddressOf DownloadProgress

    Try
        DownloadClient.DownloadFileAsync(New Uri(ArchiveUrl), Path.FullName)
    Catch ex As WebException
        ' never throws an exception 
        If ex.Response Is Nothing = False Then
            Dim dataStream As Stream = ex.Response.GetResponseStream
            Dim reader As New StreamReader(dataStream)
            Debug.WriteLine(reader.ReadToEnd())
        Else
            Debug.WriteLine("No Response Stream")
        End If
    End Try

    'Also tried this - produces a zero size file - no errors 
    'DownloadClient.DownloadFile(New Uri(ArchiveUrl), Path.FullName)

End Sub
    
    
Public Sub DownloadProgress(sender As Object, e As DownloadProgressChangedEventArgs)
    Debug.WriteLine("Downloading: " & e.ProgressPercentage.toString)
End Sub

Public Sub DownloadComplete(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs)
    If e.Error Is Nothing = False Then
        Debug.WriteLine(e.Error.InnerException.ToString)
        Return
    End If

    Threading.Thread.Sleep(3000)
    Debug.WriteLine ("Download Complete!") 
End Sub 

【问题讨论】:

  • “我的浏览器可以工作,但我的代码不能”通常通过使您的代码看起来更像浏览器来解决:按 F12,请求文件,检查浏览器发送的标头并发送相同的标头从你的代码。我看到您已经尝试发送一些特别古老的用户代理标头-发送适用于您的浏览器的实际标头(不太可能是 IE8)-远程服务器可能认为您是机器人,而不是发送 800mb 到所以你很聪明
  • ps;你真的想将 800mb 转储到 vs 调试窗口吗?
  • 试试DownloadFileTaskAsync()Await
  • 该网站有明确的安全措施来限制自动下载。它需要请求代理的响应。 -- 使用真正的 WebBrowser 下载该文件。

标签: .net vb.net


【解决方案1】:

我试过了,效果很好

Using wc As New Net.WebClient
       wc.Headers.Add("User-Agent", "My Edgar Reader Admin@Example.com")
       wc.DownloadFile("https://www.sec.gov/Archives/edgar/daily-index/xbrl/companyfacts.zip", IO.Path.Combine(Environment.CurrentDirectory, "file.zip"))
End Using
MsgBox("Done")

我使用了你在另一篇文章中提供的 User-Agent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2020-09-02
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    相关资源
    最近更新 更多