我试图下载一个文件使用简单的基本the客户端说明,但该文件没有下载完成。如果我想下载一个10 an /100 an文件,它要么下载一个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发布于 2017-03-02 17:59:40
最后,我通过这个论坛找到了一个解决方案。我只是在webclient中添加了标题,它运行得很好。这是供其他人参考的代码。
wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)")在调用DownloadFileAsync函数之前添加它。
https://stackoverflow.com/questions/42485441
复制相似问题