我正在尝试使用简单的基本WebClient指令下载文件,但是该文件未下载完成。如果我想下载10MB/100MB文件,它要么下载7kb文件或空文件。我只是在使用progressbar来显示下载进度。这是我正在使用的代码。
Imports System.Net
Public Class Form1
Dim WithEvents wc As New WebClient
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
wc.DownloadFileAsync(New Uri("http://cachefly.cachefly.net/100mb.test"), "100mb.test")
End Sub
Private Sub wc_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles wc.DownloadFileCompleted
ProgressBar1.Visible = False
ProgressBar1.Value = 0
End Sub
Private Sub wc_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles wc.DownloadProgressChanged
ProgressBar1.Visible = True
ProgressBar1.Value = e.ProgressPercentage
End Sub
End Class
最后,我通过此论坛找到了一个解决方案。我刚刚将标题添加到网络电视机中,并且效果很好。这是其他参考的代码。
wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)")
在调用下载fileasync函数之前添加它。