The remote server returned an error: (502) Bad Gateway while by passing proxy in RRS feed

  • Question

  • I am trying to bypass proxy using below code... But it is showing me error i.e. ("The remote server returned an error: (502) Bad Gateway."). In exception i found error ("The proxy could not connect to the destination in time") and status as "protocol error 7". I searched a lot on internet but didnt get solution. Here's my code for bypassing proxy...

    Function ConnectViaHTTPProxy() As TcpClient
        Dim SOCKET1 As System.Net.Sockets.Socket
            Dim request As HttpWebRequest = WebRequest.Create("Web Address--") 
            Dim cred As String = "ProxyServer:Port--"

            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
    Can you please tell me where i am wrong....
    Tuesday, August 25, 2015 10:42 AM