我想循环访问一个数组并在每次迭代中执行httpwebrequest。 该代码似乎工作,但它会暂停一段时间(例如比设置的超时时间更长,尝试将其设置为100来检查并且它仍然暂停),然后继续进行工作。VB.net httpwebrequest For循环挂起每10个左右的迭代
这里是我到目前为止有:
For i As Integer = 0 To numberOfProxies - 1
Try
'create request to a proxyJudge php page using proxy
Dim request As HttpWebRequest = HttpWebRequest.Create("http://www.pr0.net/deny/azenv.php")
request.Proxy = New Net.WebProxy(proxies(i)) 'select the current proxie from the proxies array
request.Timeout = 10000 'set timeout
Dim response As HttpWebResponse = request.GetResponse()
Dim sr As StreamReader = New StreamReader(response.GetResponseStream())
Dim pageSourceCode As String = sr.ReadToEnd()
'check the downloaded source for certain phrases, each identifies a type of proxy
'HTTP_X_FORWARDED_FOR identifies a transparent proxy
If pageSourceCode.Contains("HTTP_X_FORWARDED_FOR") Then
'delegate method for cross thread safe
UpdateListbox(ListBox3, proxies(i))
ElseIf pageSourceCode.Contains("HTTP_VIA") Then
UpdateListbox(ListBox2, proxies(i))
Else
UpdateListbox(ListBox1, proxies(i))
End If
Catch ex As Exception
'MessageBox.Show(ex.ToString) used in testing
UpdateListbox(ListBox4, proxies(i))
End Try
completedProxyCheck += 1
lblTotalProxiesChecked.CustomInvoke(Sub(l) l.Text = completedProxyCheck)
Next
我已经找遍了这个网站,并通过谷歌,以及这类问题的大多数答复说,响应必须关闭。我已经尝试了使用块,如:
Using response As HttpWebResponse = request.GetResponse()
Using sr As StreamReader = New StreamReader(response.GetResponseStream())
Dim pageSourceCode As String = sr.ReadToEnd()
'check the downloaded source for certain phrases, each identifies a type of proxy
'HTTP_X_FORWARDED_FOR identifies a transparent proxy
If pageSourceCode.Contains("HTTP_X_FORWARDED_FOR") Then
'delegate method for cross thread safe
UpdateListbox(ListBox3, proxies(i))
ElseIf pageSourceCode.Contains("HTTP_VIA") Then
UpdateListbox(ListBox2, proxies(i))
Else
UpdateListbox(ListBox1, proxies(i))
End If
End Using
End Using
而且它没有什么区别(虽然我可能已经实现了这是错误的)你可以告诉IM到VB很新或任何OOP所以它可能是一个简单的问题,但我不能解决它。
任何意见或只是提示如何诊断这些类型的问题将非常感激。编辑: 现在即时通讯彻底困惑。 try catch语句是否自动关闭响应,还是我需要在finally中放置一些东西?如果是这样,什么?我不能使用response.close(),因为它在try块中声明。
也许我只是使用非常糟糕的结构化代码,并有更好的方法来做到这一点?或其他什么导致暂停/挂起?
关于第二块代码,这里并没有涉及到:“使用块的行为就像Try ...最后的构造,其中Try块使用资源,Finally块处理它们。因此,无论您如何退出块,Using块都保证资源的处理,即使在未处理的异常情况下也是如此,除了StackOverflowException。“请参阅MSDN http://msdn.microsoft.com/en-us/library/htd05whh%28v=VS.100%29.aspx – apros
好吧,所以最后看看那个和try catch的相关页面,看起来我的原创代码是关闭响应的权利?或者我需要添加一些东西最后 - 我试着在那里添加response.close(),但它不起作用,因为在try块中声明了响应。 – Steve
但是,您可以轻松地在try块之外声明responese对象,然后在try-catch-finally中实例化它。这将帮助你在finally块中使用response.close()块 – apros