我有带有 HttpWebRequest 的 VB 代码,它收集了数百个网站的 html,但需要很长时间才能完成任务。代码基本上是一个 for-to-loop 并读取列表框中每个网站的 html。在一个循环中,在每个网站的提取的 html 中搜索特定的单词。我想显示每个单词列下都有单词的网站列表。
For Each webAddr As String In lstbox.Items
strHtml = Make_A_Call(webAddr)
If strHtml.Contains("Keyword1") Then
..........
End If
If strHtml.Contains("Keyword2") Then
..........
End If
..........
..........
..........
..........
..........
Next
Private Function Make_A_Call(ByVal strURL As String) As String
Dim strResult As String
Dim wbrq As HttpWebRequest
Dim wbrs As HttpWebResponse
Dim sr As StreamReader
Try
strResult = ""
wbrq = WebRequest.Create(strURL)
wbrq.Method = "GET"
' Read the returned data
wbrs = wbrq.GetResponse
sr = New StreamReader(wbrs.GetResponseStream)
strResult = sr.ReadToEnd.Trim
sr.Close()
sr.Dispose()
wbrs.Close()
Catch ex As Exception
ErrMessage.Text = ex.Message.ToString
ErrMessage.ForeColor = Color.Red
End Try
Return strResult
End Function
编译后的代码需要将近 5 分钟才能完成循环。有时它无法完成。是否可以对其进行修改以提高性能。请帮助提供更好的代码和建议。