0

我目前正在开发一个 VB.net 项目,我需要从某个 URI 获取 http 响应,但请求需要通过 http 代理,我对此非常满意。当我意识到有时我们的代理服务器无法正常工作然后应用程序抛出错误时,问题就出现了。我希望我的应用程序检查代理是否工作,如果没有,那么我希望它从代理列表/数组中获取另一个代理。另外,如果您有任何其他想法,请随时分享。

目前我正在使用它(它是静态的,当它引发错误时,我需要手动更改代理):

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

我想要的是这样的:

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

仅供参考:我的代理不需要身份验证。

如果我不能正确解释它,我很抱歉,老实说,我在 VB.net 中相当新。

非常感谢您的时间和耐心。感谢你的帮助。

4

1 回答 1

0

借用这个问题

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
于 2016-10-04T17:53:57.667 回答