本文介绍了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

尝试

FileStreamer =新的FileStream(fileDestination,FileMode.Create)

URLReq = WebRequest.Create(sURL)
URLRes = URLReq.GetResponse'错误发生在这里!
sChunks = URLReq.GetResponse.GetResponseStream
DownloadProgressBar.Value = 0
DownloadProgressBar.Maximum = URLRes.ContentLength

Do
iBytesRead = sChunks.Read bBuffer,0,1000)
FileStreamer.Write(bBuffer,0,iBytesRead)
循环直到iBytesRead = 0

sChunks.Close()
FileStreamer.Close )
URLRes.Close()

返回True
Catch ex As Exception
返回False
结束尝试

结束函数

这对前几个文件可以正常工作。但是,它开始在 URLReq.GetResponse 行发出以下错误:

任何人知道可能导致这种情况?

解决方案

如果您要定位1.1框架并运行多个并发请求,请尝试设置 System.Net.ServicePointManager.DefaultConnectionLimit p>

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:

Anyone know what could be causing this?

解决方案

If you're targeting the 1.1 framework and are running multiple concurrent requests try setting System.Net.ServicePointManager.DefaultConnectionLimit

这篇关于HttpWebRequest“操作已超时”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:14