VB.net httpwebrequest For loop hangs every 10 or so iterations

Posted by user574632 on Stack Overflow See other posts from Stack Overflow or by user574632
Published on 2011-01-14T08:54:56Z Indexed on 2011/01/14 9:53 UTC
Read the original article Hit count: 300

Hello, I am trying to loop through an array and perform an httpwebrequest in each iteration. The code seems to work, however it pauses for a while (eg alot longer than the set timeout. Tried setting that to 100 to check and it still pauses) after every 10 or so iterations, then carries on working.

Here is what i have so far:

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

I have searched all over this site and via google, and most responses to this type of question say the response must be closed. I have tried a using block, eg:

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

And it makes no difference (though i may have implemented it wrong) As you can tell im very new to VB or any OOP so its probably a simple problem but i cant work it out.

Any suggestions or just tips on how to diagnose these types of problems would be really appreciated.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about httpwebrequest