How to use Proxies?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How to use Proxies?

  1. #1
    Join Date
    Jan 2009
    Posts
    4

    Angry How to use Proxies?

    Hello,

    Proxies are HTTP and socks. How can I use them to surf internet?

    I know "iwebproxy " of .net but do not understand how to use them for this perpose.

    Any guidance will be appriciated.

    Thanks
    Last edited by memo421; April 2nd, 2009 at 12:28 AM.

  2. #2
    Join Date
    Jul 2003
    Posts
    135

    Re: How to use Proxies?

    Create a new form. Put a webbrowser object on it then use this code. Change the "SetupProxy" arguments as you need:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If SetupProxy(False) Then
                WebBrowser1.Navigate("http://www.yahoo.com/")
            End If
        End Sub
    
        Public Function SetupProxy(ByVal UseProxy As Boolean, Optional ByVal ProxyName As String = "", Optional ByVal ProxyPort As Integer = 0, Optional ByVal UserName As String = "", Optional ByVal UserPassword As String = "", Optional ByVal UserDomain As String = "") As Boolean
            ' Create our proxy settings
            If UseProxy = False Then
                Net.WebRequest.DefaultWebProxy = New Net.WebProxy
            Else
                If (ProxyName = "") Or (ProxyPort = 0) Then
                    If UseProxy = True Then
                        MsgBox("Either the proxy name or port number was left blank but the option to use a proxy server is checked.  Please review your settings before updating!")
                    End If
                    SetupProxy = False
                Else
                    Dim ProxyObject As New System.Net.WebProxy(ProxyName & ":" & ProxyPort, True)
                    Dim cred As Net.NetworkCredential = New Net.NetworkCredential(UserName, UserPassword, UserDomain)
                    ProxyObject.Credentials = cred
                    Net.WebRequest.DefaultWebProxy = ProxyObject
                    SetupProxy = True
                End If
            End If
        End Function
    End Class
    Just typed this up real quick so it needs testing but it should give you a good start. All web requests in your application should follow what is set here so you can have a form that lets them select "direct connect" (no proxy), use default system proxy, or enter their own.

    For use system proxy you can pull whats in the registry with something like this:

    Code:
        Private Function ImportSystemSettings() As String
            ' Get the IE proxy settings
            Dim myProxy As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", "").ToString
            If myProxy = "" Then
                MsgBox("I could not find any proxy server information in the registry.  If a proxy server is required please manually enter the information.", MsgBoxStyle.OkOnly, "No Proxy Info Found")
                Return ""
            Else
                Return myProxy
            End If
        End Function
    Which will get you ProxyName:PortNumber which you will split apart (based on the and use as you need.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center