HttpWebRequest“操作已超时”

编程入门 行业动态 更新时间:2024-10-26 12:32:47
本文介绍了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:

"operation has timed out"

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“操作已超时”

本文发布于:2023-11-05 19:40:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   HttpWebRequest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!