HttpWebRequest“操作已经超时”

时间:2011-05-26 13:44:16

标签: vb.net httpwebrequest download

我在我的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行上发出以下错误:

  

“操作已超时”

任何人都知道造成这种情况的原因是什么?

2 个答案:

答案 0 :(得分:2)

如果您的目标是1.1框架并且正在运行多个并发请求,请尝试设置System.Net.ServicePointManager.DefaultConnectionLimit

答案 1 :(得分:1)

超时设置为10000毫秒(或10秒)。是否足够长的时间往返于Web服务器?

MSDN Documentation表示默认值为100秒(100000毫秒)是否有任何理由更改了默认设置?