使用.NET WebClient调用DownloadFileAsync与DownloadFile无响应不抛异常求助
问题解决方案
核心问题点
- 回调事件绑定错误:你调用的是
DownloadFileAsync方法,对应的完成回调事件是DownloadFileCompleted,而非你当前代码绑定的DownloadDataCompleted,这是你看不到任何下载完成/报错提示的核心原因。 - User-Agent被站点拦截:sec.gov有严格的反爬校验规则,你使用的
User-Agent: Other属于无效标识,会被服务器直接拦截请求,不会返回文件内容。同步下载生成空文件就是因为拦截后无有效响应内容写入。 - 额外注意:如果是控制台场景运行代码,调用异步下载后不要让主线程直接退出,否则进程终止会直接中断下载且无任何提示。
修正后代码示例
Public DownloadClient As WebClient ' 控制台场景可加信号量等待下载完成 Private ReadOnly downloadResetEvent As New Threading.ManualResetEvent(False) Public Sub DownloadAsync() 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" Dim savePath As String = System.IO.Path.Combine(UserFolder, ArchiveTemp) If DownloadClient IsNot Nothing AndAlso DownloadClient.IsBusy Then Debug.WriteLine("File download in progress") Return End If Debug.Print("Downloading to: " & savePath) ' 提前配置安全协议,建议放在程序启动入口执行一次即可 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls13 ServicePointManager.DefaultConnectionLimit = 10 DownloadClient = New WebClient() ' 替换为正常浏览器UA DownloadClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") ' 补充必要请求头避免拦截 DownloadClient.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8") DownloadClient.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2") DownloadClient.Headers.Add("Accept-Encoding", "gzip, deflate, br") ' 修正事件绑定:对应DownloadFileAsync的回调事件 AddHandler DownloadClient.DownloadFileCompleted, AddressOf DownloadComplete AddHandler DownloadClient.DownloadProgressChanged, AddressOf DownloadProgress Try downloadResetEvent.Reset() DownloadClient.DownloadFileAsync(New Uri(ArchiveUrl), savePath) ' 控制台场景打开下面这句等待下载完成,WinForm/WPF场景不需要 ' downloadResetEvent.WaitOne() Catch ex As WebException If ex.Response IsNot Nothing Then Dim dataStream As IO.Stream = ex.Response.GetResponseStream() Dim reader As New IO.StreamReader(dataStream) Debug.WriteLine(reader.ReadToEnd()) Else Debug.WriteLine("No Response Stream") End If End Try End Sub Public Sub DownloadProgress(sender As Object, e As DownloadProgressChangedEventArgs) Debug.WriteLine($"Downloading: {e.ProgressPercentage}%,已下载{e.BytesReceived}/{e.TotalBytesToReceive}字节") End Sub Public Sub DownloadComplete(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) If e.Error IsNot Nothing Then Debug.WriteLine($"下载出错:{e.Error.ToString()}") downloadResetEvent.Set() Return End If If e.Cancelled Then Debug.WriteLine("下载被取消") downloadResetEvent.Set() Return End If Threading.Thread.Sleep(3000) Debug.WriteLine("Download Complete!") downloadResetEvent.Set() End Sub
额外优化建议
如果经常遇到下载超时问题,可以自定义继承WebClient的子类,设置更长的超时时间:
Public Class CustomWebClient Inherits WebClient Public Property Timeout As Integer = 600000 ' 单位毫秒,这里设为10分钟 Protected Overrides Function GetWebRequest(address As Uri) As WebRequest Dim request = MyBase.GetWebRequest(address) request.Timeout = Timeout CType(request, HttpWebRequest).ReadWriteTimeout = Timeout Return request End Function End Class
使用时替换原来的WebClient为CustomWebClient即可。
内容的提问来源于stack exchange,提问作者Berries
相关产品推荐
相关产品推荐

