HttpWebRequest“操作已超时”
我在 Windows 应用程序中使用 httpWebRequest 将文件从网络服务器 (sURL) 下载到本地文件夹 (fileDestination),如下所示:
Public Function DownloadFile(ByVal sURL As String, _
ByVal fileDestination As String, _
ByVal WebRequestType As String) As Boolean
Dim URLReq As HttpWebRequest
Dim URLRes As HttpWebResponse
Dim FileStreamer As FileStream
Dim bBuffer(999) As Byte
Dim iBytesRead As Integer
Dim folderDestination As String
Dim sChunks As Stream
Try
FileStreamer = New FileStream(fileDestination, FileMode.Create)
URLReq = WebRequest.Create(sURL)
URLRes = URLReq.GetResponse 'Error occurs here!!
sChunks = URLReq.GetResponse.GetResponseStream
DownloadProgressBar.Value = 0
DownloadProgressBar.Maximum = URLRes.ContentLength
Do
iBytesRead = sChunks.Read(bBuffer, 0, 1000)
FileStreamer.Write(bBuffer, 0, iBytesRead)
Loop Until iBytesRead = 0
sChunks.Close()
FileStreamer.Close()
URLRes.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
这对于前几个文件来说效果很好。但随后它开始在 URLReq.GetResponse
行上给出以下错误:
“操作已超时”
有人知道这可能是什么原因造成的吗?
I am using an httpWebRequest in my windows app to download files from a webserver (sURL), to a local folder (fileDestination) as such:
Public Function DownloadFile(ByVal sURL As String, _
ByVal fileDestination As String, _
ByVal WebRequestType As String) As Boolean
Dim URLReq As HttpWebRequest
Dim URLRes As HttpWebResponse
Dim FileStreamer As FileStream
Dim bBuffer(999) As Byte
Dim iBytesRead As Integer
Dim folderDestination As String
Dim sChunks As Stream
Try
FileStreamer = New FileStream(fileDestination, FileMode.Create)
URLReq = WebRequest.Create(sURL)
URLRes = URLReq.GetResponse 'Error occurs here!!
sChunks = URLReq.GetResponse.GetResponseStream
DownloadProgressBar.Value = 0
DownloadProgressBar.Maximum = URLRes.ContentLength
Do
iBytesRead = sChunks.Read(bBuffer, 0, 1000)
FileStreamer.Write(bBuffer, 0, iBytesRead)
Loop Until iBytesRead = 0
sChunks.Close()
FileStreamer.Close()
URLRes.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
This works fine for the first few files. But then it starts giving the following error on the URLReq.GetResponse
line:
"operation has timed out"
Anyone know what could be causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标是 1.1 框架并且正在运行多个并发请求,请尝试设置 System.Net.ServicePointManager.DefaultConnectionLimit
If you're targeting the 1.1 framework and are running multiple concurrent requests try setting
System.Net.ServicePointManager.DefaultConnectionLimit
超时设置为 10000 毫秒(或 10 秒)。对于到 Web 服务器的往返来说,这个时间是否足够长?
MSDN 文档表示默认值为 100 秒(100000 毫秒)您更改默认值有什么原因吗?
The timeout is set to 10000 milliseconds (or 10 seconds). Is that long enough for the roundtrip to the web server?
The MSDN Documentation says the default is 100 seconds (100000 ms) is there any reason you changed that default?