我一直在vb.net中制作一个下载器,一切正常除了下载之外,下载开始大约需要10秒 这是正常现象还是我做错了什么? 下载是在线程中完成的,之前当我尝试在线程之外下载时,程序在这 10 秒内没有响应 非常感谢任何帮助 :) 叛逆叛逆
代码:
download = New WebClient
download.DownloadFileAsync(New Uri(File to DDR ), download)
我用什么来修复它,感谢 AVD! 在代码中将 Web 客户端代理设置为空
我的代码:
download = New WebClient
download.DownloadFileAsync(New Uri(File to DDR ), download)
我添加了什么来修复它
download.proxy = Nothing
更新: 为了适应可能使用代理的用户,此代码将获取系统代理
download.Proxy = System.Net.HttpWebRequest.DefaultWebProxy.Credentials
希望这有帮助!
这是我的一些项目的下载器代码。您可以将凭据留空。
Try
Dim URI As String = "Your URL Starting from http"
Dim oRequest As System.Net.HttpWebRequest = CType(HttpWebRequest.Create(URI), HttpWebRequest)
oRequest.Credentials = New System.Net.NetworkCredential("", "")
Using oResponse As System.Net.WebResponse = CType(oRequest.GetResponse, System.Net.WebResponse)
Using responseStream As IO.Stream = oResponse.GetResponseStream
Using fs As New IO.FileStream("C:\mydownload\Example.zip", FileMode.Create, FileAccess.Write)
Dim buffer(2047) As Byte
Dim read As Integer
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
oResponse.Close()
End Using
MsgBox("Download Complete")
Catch ex As Exception
MsgBox(Err.Description)
End Try