HttpWebRequest“操作已超时”

发布于 2024-11-10 04:32:54 字数 1423 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-11-17 04:32:54

如果您的目标是 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

不交电费瞎发啥光 2024-11-17 04:32:54

超时设置为 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文