Check proxy's from listbox

2019-09-20 06:04发布

问题:

So i have a listbox that i have there for a list of proxies. I have 4 buttons pertaining to it. they are find, load, save and check

I have the first 3 finished and working but i haven't found anything useful pertaining to checking the proxies, the only one that i found took like 6 seconds per proxy so it took a lot of time for a decent sized list.

So how could i make it that on the press of that button, it checks all of the proxies in the listbox and it deletes the slow ones and the ones that flat out do not work. and does this at a decent pace(so it would probably be multi threaded)

and since i can not figure this out i have no code pertaining to this except for the sub for the button click i do not feel there is a need to post code

回答1:

My suggestion for you is :

1)use a timer control and set it's Tick property to an appropriate value such 500;

2) create an array of BackGroudWorkers for example BackGroudWorker[20];

3)when your app start run all BackGroudWorkers in array and in tick event of Timer check if any of this BackGroudWorker completed or not.If completed and you have other item in list then run it with new Item.Do this until all list Items checked



回答2:

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

You should manage your code for threads etc, as suggested by @Nima for your proxy checking problem I have 2 methods here One asks proxy string and tries to connect it. e.g.

ProxyStatus = CheckProxy("http://192.168.1.1:8080/")

ProxyStatus is True/False depending on if proxy works or Not