0

I am currently working on a VB.net project where I need to get http responses from a certain URI but the requests needs to go through http proxy which I am perfectly fine with. The problem occurred when I realised sometimes our proxy servers are not working and then the application throws an error. I want my app to check whether the proxy is working or not, if not then I want it to take another proxy from the proxy list/array. And also, please feel free to share if you have any alternative ideas.

Currently I am using this (which is static and when it throws an error I need to manually change the proxy):

Dim proxyObject As WebProxy = New WebProxy("192.168.0.10:80")
request.Proxy = proxyObject

What I want is something like this:

If WebProxy("192.168.0.10:80") is working fine Then
    Execute the response
Else 
    Take the next proxy address from the list/array and go back to the starting 
    of "If"
End If

FYI: my proxies doesn't require authentication.

I apologise if I couldn't explain it properly and to be honest I'm fairly new in VB.net.

Thanks a lot for your time and patience. Appreciate your help.

Sabbir Hassan
  • 175
  • 14

1 Answers1

0

Borrowing from this question

Dim urlList As New List(Of String)  'Urls stored here

For each urlString as string in urlList
    If CheckProxy(urlString) Then
        'Execute the response
    else
         Continue For 'or something else here, mark it as bad?
    End If
next

    Public Shared Function CheckProxy(ByVal Proxy As String) As Boolean
        Dim prx As Uri = Nothing
        If Uri.TryCreate(Proxy, UriKind.Absolute, prx) Then
            Return CheckProxy(prx)
        ElseIf Uri.TryCreate("http://" & Proxy, UriKind.Absolute, prx) Then
            Return CheckProxy(prx)
        Else
            Return False
        End If
    End Function

    Public Shared Function CheckProxy(ByVal Proxy As Uri) As Boolean
        Dim iProxy As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        iProxy.ReceiveTimeout = 500 : iProxy.SendTimeout = 500
        Try
            '' Connect using a timeout (1/2 second)
            Dim result As IAsyncResult = iProxy.BeginConnect(Proxy.Host, Proxy.Port, Nothing, Nothing)
            Dim success As Boolean = result.AsyncWaitHandle.WaitOne(500, True)
            If (Not success) Then
                iProxy.Close() : Return False
            End If
        Catch ex As Exception
            Return False
        End Try

        Dim bytData() As Byte, strData As String
        Dim iDataLen As Integer = 1024
        strData = String.Format("CONNECT {0}:{1} HTTP/1.0{2}{2}", "www.google.com", 80, vbNewLine)

        bytData = System.Text.ASCIIEncoding.ASCII.GetBytes(strData)
        If iProxy.Connected Then
            iProxy.Send(bytData, bytData.Length, SocketFlags.None)
            ReDim bytData(1024)
            Do
                Try
                    iDataLen = iProxy.Receive(bytData, bytData.Length, SocketFlags.None)
                Catch ex As Exception
                    iProxy.Close() : Return False
                End Try
                If iDataLen > 0 Then
                    strData = System.Text.ASCIIEncoding.ASCII.GetString(bytData)
                    Exit Do
                End If
            Loop
        Else
            Return False
        End If
        iProxy.Close()

        Dim strAttribs() As String
        strAttribs = strData.Split(" "c)
        If strAttribs(1).Equals("200") Then
            Return True
        Else
            Return False
        End If
    End Function
Community
  • 1
  • 1
Jimmy Smith
  • 2,452
  • 1
  • 16
  • 19