Created 14th. october 2005

Detecting and getting proxy windows proxy settings for futher use in eg. a call to a Webservice, a Webresponse or WebRequest object.


Public Class cDetectProxy

  Public Function GetProxy(ByVal URL As String) As System.Net.WebProxy

    Dim PX As System.Net.WebProxy

    Try
      Dim bFound As Boolean = False
      Dim sTemp As String
      Dim str() As String
 
      Dim UseProxy As Boolean = CBool(GetRegistrySetting("ProxyEnable", False))
 
      If UseProxy Then
        Dim S As New Uri(URL)
        If Not IsByPassed(S.Host) Then
          sTemp = GetRegistrySetting("ProxyServer", String.Empty)
          sTemp = Replace(sTemp, "http://", String.Empty)
 
          If InStr(1, sTemp, ":") > 0 Then
            str = Split(sTemp, ":")
            If UBound(str) = 1 Then
              PX = New System.Net.WebProxy
              PX.Address = New System.Uri("http://" & str(0) & ":" & str(1))
              PX.Credentials = System.Net.CredentialCache.DefaultCredentials
            End If
          End If
        End If
      End If
    Catch ex As System.Exception
      Err.Clear()
    End Try
 
    Return PX

  End Function

  Private Function IsByPassed(ByVal ServerName As String)
    Try
      Dim sBypassStr As String = GetRegistrySetting("ProxyOverride", String.Empty)
      If sBypassStr <> String.Empty Then
 
        sBypassStr = Replace(sBypassStr, vbCrLf, String.Empty)
 
        Dim HE As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(ServerName)
        Dim sBypassList As String() = Split(sBypassStr, ";")
 
        For i As Integer = 0 To sBypassList.Length - 1
          If UCase(ServerName) Like UCase(Trim(sBypassList(i))) OrElse CStr(HE.AddressList(0).ToString) Like sBypassList(i) Then
            Return True
          End If
        Next i
      End If
    Catch ex As System.Exception
      Err.Clear()
      Return False
    End Try
  End Function
 
  Private Function GetRegistrySetting(ByVal Key As String, ByVal DefaultValue As Object) As Object
    Dim oReg As Microsoft.Win32.Registry, oRegKey As Microsoft.Win32.RegistryKey 

    Dim FormFolder As String = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"

    Try
      oReg.CurrentUser.CreateSubKey(FormFolder)
      oRegKey = oReg.CurrentUser.OpenSubKey(FormFolder, False)
 
      Return oRegKey.GetValue(Key, String.Empty)
    Catch EX As Exception
      Err.Clear()
    Finally
      oRegKey.Close() : oRegKey = Nothing
      oReg = Nothing
    End Try

  End Function

 End Class

You can use it like this:

    Dim DP As New cDetectProxy 
    Dim PX As System.Net.WebProxy = DP.GetProxy("http://www.kamp-hansen.dk/")
    If Not PX Is Nothing Then
      ' Here you can add the proxy to different object like:
      ' WebResponse
      ' WebRequest
      ' WebService Reference
    End If


I really hope, that you will find this class usefull. Feel free to use it as you would like, but don't forget to mention the author. ;o)

 


How to you think this article is?
Please select from 1 to 5 stars.
Submit rating.