我正在为一个特定的网站写一个网络爬虫。该应用程序是一个VB.NET Windows窗体应用程序,它是
不
使用多线程-每个Web请求都是连续的。但是,在成功地检索了10个页面之后,每个连续的请求都超时。
我已经回顾了在这里发布的类似问题,并在getpage例程中实现了推荐的技术,如下所示:
Public Function GetPage(ByVal url As String) As String
Dim result As String = String.Empty
Dim uri As New Uri(url)
Dim sp As ServicePoint = ServicePointManager.FindServicePoint(uri)
sp.ConnectionLimit = 100
Dim request As HttpWebRequest = WebRequest.Create(uri)
request.KeepAlive = False
request.Timeout = 15000
Try
Using response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse)
Using dataStream As Stream = response.GetResponseStream()
Using reader As New StreamReader(dataStream)
If response.StatusCode <> HttpStatusCode.OK Then
Throw New Exception("Got response status code: " + response.StatusCode)
End If
result = reader.ReadToEnd()
End Using
End Using
response.Close()
End Using
Catch ex As Exception
Dim msg As String = "Error reading page """ & url & """. " & ex.Message
Logger.LogMessage(msg, LogOutputLevel.Diagnostics)
End Try
Return result
End Function
我错过什么了吗?我是否没有关闭或处置一个应该关闭或处置的对象?奇怪的是,它总是发生在连续10个请求之后。
笔记:
在这个方法所在的类的构造函数中,我有以下内容:
ServicePointManager.DefaultConnectionLimit=100
如果我将keepalive设置为true,则超时将在五个请求之后开始。
所有请求都针对同一域中的页面。
编辑
我在每个Web请求之间添加了两到七秒的延迟,这样我就不会看起来像是在“锤击”站点或试图进行DoS攻击。但是,问题仍然存在。