我试图使用下面的代码绕过代理......但它显示我的错误,即("远程服务器返回错误:(502)Bad Gateway。")。在内部异常中我发现错误("代理无法及时连接到目的地")。我在互联网上搜索了很多,但没有得到解决方案。 这是我绕过代理的代码...
Function ConnectViaHTTPProxy() As TcpClient
Dim SOCKET1 As System.Net.Sockets.Socket
Try
Dim request As HttpWebRequest = WebRequest.Create("Web Address--10.10.10.10")
Dim cred As String = "ProxyServer:Port--http://10.10.10.10:1010/"
Dim webProxy = New WebProxy(cred)
request.UseDefaultCredentials = True
request.Proxy = webProxy
request.Timeout = 3600 * 1000
request.ReadWriteTimeout = 3600 * 1000
request.Method = "CONNECT"
Dim credentials = New NetworkCredential("UserName", "Password", "Domain_Name")
webProxy.Credentials = credentials
Dim response = request.GetResponse()
Dim responseStream = response.GetResponseStream()
Debug.Assert(responseStream IsNot Nothing)
Const Flags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance
Dim rsType = responseStream.[GetType]()
Dim connectionProperty = rsType.GetProperty("Connection", Flags)
Dim connection = connectionProperty.GetValue(responseStream, Nothing)
Dim connectionType = connection.[GetType]()
Dim networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags)
Dim networkStream = networkStreamProperty.GetValue(connection, Nothing)
Dim nsType = networkStream.[GetType]()
Dim socketProperty = nsType.GetProperty("Socket", Flags)
Dim socket = DirectCast(socketProperty.GetValue(networkStream, Nothing), Socket)
SOCKET1 = socket
SOCKET1.ReceiveTimeout = 0
SOCKET1.SendTimeout = 0
Return (New TcpClient() With {.Client = SOCKET1})
Catch ex As WebException
If ex.Status = WebExceptionStatus.ProtocolError Then
Dim resp As WebResponse = ex.Response
Using sr As New StreamReader(resp.GetResponseStream())
FileWrite(lERROR, sr.ReadToEnd().ToString(), 0)
End Using
End If
'FileWrite(lERROR, ex.InnerException.ToString(), 0)
End Try
End Function
你能告诉我我错在哪里吗?