Results 1 to 18 of 18

Thread: Proxy with Web Browser

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Proxy with Web Browser

    hi,

    I want to use Proxy with web browser. I got Code which i found some where in vb forums. Here is Code.
    Code:
    Imports System.Net
    Public Class form1
        ' The structure we use for the information
        ' to be interpreted correctly by API.
        Public Structure Struct_INTERNET_PROXY_INFO
            Public dwAccessType As Integer
            Public proxy As IntPtr
            Public proxyBypass As IntPtr
        End Structure
    
        ' The Windows API function that allows us to manipulate
        ' IE settings programmatically.
        Private Declare Auto Function InternetSetOption Lib "wininet.dll" _
        (ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, _
         ByVal lpdwBufferLength As Integer) As Boolean
    
        ' The function we will be using to set the proxy settings.
        Private Sub RefreshIESettings(ByVal strProxy As String)
            Const INTERNET_OPTION_PROXY As Integer = 38
            Const INTERNET_OPEN_TYPE_PROXY As Integer = 3
            Dim struct_IPI As Struct_INTERNET_PROXY_INFO
    
            ' Filling in structure
            struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY
            struct_IPI.proxy = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strProxy)
            struct_IPI.proxyBypass = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi("local")
    
            ' Allocating memory
            Dim intptrStruct As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
    
            ' Converting structure to IntPtr
            System.Runtime.InteropServices.Marshal.StructureToPtr(struct_IPI, intptrStruct, True)
            Dim iReturn As Boolean = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
        End Sub
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     Dim sr As New StreamReader("d:\p.txt")
            Dim ar As New ArrayList
            While sr.Peek <> -1
                ar.Add(sr.ReadLine.ToString)
            End While
            Dim rn As New Random
            Dim pr As Integer
            pr = rn.Next(ar.Count)
    
            RefreshIESettings(ar.Item(pr).ToString)
     ListBox1.Items.Add(ar.Item(pr).ToString)
            WebBrowser1.AllowNavigation = True
            WebBrowser1.AllowWebBrowserDrop = True
            WebBrowser1.ScriptErrorsSuppressed = True
            System.Threading.Thread.Sleep(5000)
            WebBrowser1.Navigate("http:\\www.whatismyip.com")
    Its not Working for me.. Please Help me. Random Proxy is added correctly in Listbox. Please Help me I will Add Rep.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Proxy with Web Browser

    Below code does the job but needs restart of Internet Explorer every time you Enable or Disable the settings.

    vb.net Code:
    1. Imports Microsoft.Win32
    2.  
    3. Public Class Form1
    4.  
    5.     Public Sub EnableProxy( _
    6.         ByVal proxyAddress As String, _
    7.         ByVal proxyPort As Integer, _
    8.         ByVal bypassLocal As Boolean _
    9.     )
    10.  
    11.         Dim regKey As RegistryKey = Nothing
    12.  
    13.         Try
    14.             regKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", True)
    15.  
    16.             regKey.SetValue("ProxyEnable", True, RegistryValueKind.DWord)
    17.             regKey.SetValue("ProxyServer", proxyAddress + ":" + proxyPort.ToString, RegistryValueKind.String)
    18.  
    19.             If bypassLocal Then
    20.                 regKey.SetValue("ProxyOverride", "<local>", RegistryValueKind.String)
    21.             Else
    22.                 regKey.DeleteValue("ProxyOverride", False)
    23.             End If
    24.         Catch ex As Exception
    25.             MessageBox.Show(ex.Message)
    26.         Finally
    27.             If regKey IsNot Nothing Then
    28.                 regKey.Close()
    29.             End If
    30.             MessageBox.Show("Done")
    31.         End Try
    32.     End Sub
    33.  
    34.     Public Sub DisableProxy()
    35.         Dim regKey As RegistryKey = Nothing
    36.  
    37.         Try
    38.             regKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", True)
    39.             regKey.SetValue("ProxyEnable", False, RegistryValueKind.DWord)
    40.         Catch ex As Exception
    41.             MessageBox.Show(ex.Message)
    42.         Finally
    43.             If regKey IsNot Nothing Then
    44.                 regKey.Close()
    45.             End If
    46.             MessageBox.Show("Done")
    47.         End Try
    48.     End Sub
    49.  
    50.     Private Sub Button1_Click( _
    51.         ByVal sender As System.Object, _
    52.         ByVal e As System.EventArgs _
    53.     ) Handles Button1.Click
    54.  
    55.         EnableProxy("111.222.333.444", 80, False)
    56.     End Sub
    57.  
    58.     Private Sub Button2_Click( _
    59.         ByVal sender As System.Object, _
    60.         ByVal e As System.EventArgs _
    61.     ) Handles Button2.Click
    62.  
    63.         DisableProxy()
    64.     End Sub
    65.  
    66. End Class

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Proxy with Web Browser

    Quote Originally Posted by Deepak Sakpal
    Below code does the job but needs restart of Internet Explorer every time you Enable or Disable the settings.

    vb.net Code:
    1. Imports Microsoft.Win32
    2.  
    3. Public Class Form1
    4.  
    5.     Public Sub EnableProxy( _
    6.         ByVal proxyAddress As String, _
    7.         ByVal proxyPort As Integer, _
    8.         ByVal bypassLocal As Boolean _
    9.     )
    10.  
    11.         Dim regKey As RegistryKey = Nothing
    12.  
    13.         Try
    14.             regKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", True)
    15.  
    16.             regKey.SetValue("ProxyEnable", True, RegistryValueKind.DWord)
    17.             regKey.SetValue("ProxyServer", proxyAddress + ":" + proxyPort.ToString, RegistryValueKind.String)
    18.  
    19.             If bypassLocal Then
    20.                 regKey.SetValue("ProxyOverride", "<local>", RegistryValueKind.String)
    21.             Else
    22.                 regKey.DeleteValue("ProxyOverride", False)
    23.             End If
    24.         Catch ex As Exception
    25.             MessageBox.Show(ex.Message)
    26.         Finally
    27.             If regKey IsNot Nothing Then
    28.                 regKey.Close()
    29.             End If
    30.             MessageBox.Show("Done")
    31.         End Try
    32.     End Sub
    33.  
    34.     Public Sub DisableProxy()
    35.         Dim regKey As RegistryKey = Nothing
    36.  
    37.         Try
    38.             regKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", True)
    39.             regKey.SetValue("ProxyEnable", False, RegistryValueKind.DWord)
    40.         Catch ex As Exception
    41.             MessageBox.Show(ex.Message)
    42.         Finally
    43.             If regKey IsNot Nothing Then
    44.                 regKey.Close()
    45.             End If
    46.             MessageBox.Show("Done")
    47.         End Try
    48.     End Sub
    49.  
    50.     Private Sub Button1_Click( _
    51.         ByVal sender As System.Object, _
    52.         ByVal e As System.EventArgs _
    53.     ) Handles Button1.Click
    54.  
    55.         EnableProxy("111.222.333.444", 80, False)
    56.     End Sub
    57.  
    58.     Private Sub Button2_Click( _
    59.         ByVal sender As System.Object, _
    60.         ByVal e As System.EventArgs _
    61.     ) Handles Button2.Click
    62.  
    63.         DisableProxy()
    64.     End Sub
    65.  
    66. End Class

    I have nearly 100 Proxy's. I want to use random, proxy each time whenever new website loads. Then do i ned to call EnableProxy each and every time?

    It is not Working for me.. is there any problem with my OS?
    Last edited by yogesh12; Feb 6th, 2009 at 05:27 AM.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Proxy with Web Browser

    In my opinion it'd be a bad idea to set IE's proxy each time. That'd be something users would absolutely hate.

    You could however do something like this, which wont affect the IE proxy settings:
    VB.NET Code:
    1. Private Sub NavigateWithProxy(ByVal url As String)
    2.         Dim request As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(url), Net.HttpWebRequest)
    3.         Dim rand As New Random()
    4.         Dim reader As System.IO.StreamReader
    5.  
    6.         request.Proxy = New System.Net.WebProxy(proxyList(rand.Next(0, proxyList.Length)))
    7.         reader = New System.IO.StreamReader(request.GetResponse().GetResponseStream())
    8.         WebBrowser1.DocumentText = reader.ReadToEnd()
    9.         reader.Close()
    10.     End Sub

    Where 'proxyList' is the array or collection of proxy addresses.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Proxy with Web Browser

    Quote Originally Posted by yogesh12
    I have nearly 100 Proxy's. I want to use random, proxy each time whenever new website loads. Then do i ned to call EnableProxy each and every time?

    It is not Working for me.. is there any problem with my OS?
    No, you don't need to call it every time when new website loads. If you set it once then it will be applicable for rest of the websites that you open on later on. But if you are going choose random proxy out 100 then restarting Internet Explorer is not good.

    Need to find out some way by which changing proxy with code should take effect while Internet Explore is running. I was digging around SendMessageTimeout API but had no luck so far. Have a look at this article.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Proxy with Web Browser

    Quote Originally Posted by Deepak Sakpal
    No, you don't need to call it every time when new website loads. If you set it once then it will be applicable for rest of the websites that you open on later on. But if you are going choose random proxy out 100 then restarting Internet Explorer is not good.

    Need to find out some way by which changing proxy with code should take effect while Internet Explore is running. I was digging around SendMessageTimeout API but had no luck so far. Have a look at this article.

    My IP is Not Changing. Proxy is not working.. Can u tell me some suggestions?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Proxy with Web Browser

    Quote Originally Posted by Atheist
    In my opinion it'd be a bad idea to set IE's proxy each time. That'd be something users would absolutely hate.

    You could however do something like this, which wont affect the IE proxy settings:
    VB.NET Code:
    1. Private Sub NavigateWithProxy(ByVal url As String)
    2.         Dim request As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(url), Net.HttpWebRequest)
    3.         Dim rand As New Random()
    4.         Dim reader As System.IO.StreamReader
    5.  
    6.         request.Proxy = New System.Net.WebProxy(proxyList(rand.Next(0, proxyList.Length)))
    7.         reader = New System.IO.StreamReader(request.GetResponse().GetResponseStream())
    8.         WebBrowser1.DocumentText = reader.ReadToEnd()
    9.         reader.Close()
    10.     End Sub

    Where 'proxyList' is the array or collection of proxy addresses.

    Proxy si not working... Can u tell me some suggestion. I ma Very much frustated

  8. #8

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Proxy with Web Browser

    Quote Originally Posted by Deepak Sakpal
    Sounds like ur network settings problem. Admin might have done something to restrict proxy.
    Its my Home Computer. Nobody has control over it. I checked on other system too. all has same Result. They are showing my Systems IP.

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Proxy with Web Browser

    Quote Originally Posted by yogesh12
    Proxy si not working... Can u tell me some suggestion. I ma Very much frustated
    In what way isnt it working? I cant give you suggestions unless you give me more information.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Proxy with Web Browser

    Quote Originally Posted by Atheist
    In what way isnt it working? I cant give you suggestions unless you give me more information.

    It is showing my Real IP Address. Its not showing Proxy IP..

  12. #12
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Proxy with Web Browser

    What is showing you your real IP Address?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Proxy with Web Browser

    Quote Originally Posted by Atheist
    What is showing you your real IP Address?
    After SEtting IP Address I called webbrowser.navigate, and went to www.whatismyip.com and it always shows up only my real ip.

  14. #14
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Proxy with Web Browser

    Why are you calling WebBrowser.Navigate? Just use the subroutine i showed you above instead.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  15. #15
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Proxy with Web Browser

    Nevermind I overlooked a major flaw in my idea. The WebBrowser will need to send subsequent requests for images etc...which makes it all alot harder.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Proxy with Web Browser

    Quote Originally Posted by Atheist
    Why are you calling WebBrowser.Navigate? Just use the subroutine i showed you above instead.
    Will that web page be visible in web browser? how web browser can navigate without firing navigate event? i am nob.. please don mind my questions..

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Proxy with Web Browser

    Quote Originally Posted by Atheist
    Nevermind I overlooked a major flaw in my idea. The WebBrowser will need to send subsequent requests for images etc...which makes it all alot harder.
    I too Thought Same thing and asked.. While Posting u too posted it.. can u please give me that code?

  18. #18
    New Member
    Join Date
    Mar 2010
    Posts
    1

    Re: Proxy with Web Browser

    Quote Originally Posted by yogesh12 View Post
    hi,

    I want to use Proxy with web browser. I got Code which i found some where in vb forums. Here is Code.
    Code:
    Imports System.Net
    Public Class form1
        ' The structure we use for the information
        ' to be interpreted correctly by API.
        Public Structure Struct_INTERNET_PROXY_INFO
            Public dwAccessType As Integer
            Public proxy As IntPtr
            Public proxyBypass As IntPtr
        End Structure
    
        ' The Windows API function that allows us to manipulate
        ' IE settings programmatically.
        Private Declare Auto Function InternetSetOption Lib "wininet.dll" _
        (ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, _
         ByVal lpdwBufferLength As Integer) As Boolean
    
        ' The function we will be using to set the proxy settings.
        Private Sub RefreshIESettings(ByVal strProxy As String)
            Const INTERNET_OPTION_PROXY As Integer = 38
            Const INTERNET_OPEN_TYPE_PROXY As Integer = 3
            Dim struct_IPI As Struct_INTERNET_PROXY_INFO
    
            ' Filling in structure
            struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY
            struct_IPI.proxy = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strProxy)
            struct_IPI.proxyBypass = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi("local")
    
            ' Allocating memory
            Dim intptrStruct As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
    
            ' Converting structure to IntPtr
            System.Runtime.InteropServices.Marshal.StructureToPtr(struct_IPI, intptrStruct, True)
            Dim iReturn As Boolean = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
        End Sub
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     Dim sr As New StreamReader("d:\p.txt")
            Dim ar As New ArrayList
            While sr.Peek <> -1
                ar.Add(sr.ReadLine.ToString)
            End While
            Dim rn As New Random
            Dim pr As Integer
            pr = rn.Next(ar.Count)
    
            RefreshIESettings(ar.Item(pr).ToString)
     ListBox1.Items.Add(ar.Item(pr).ToString)
            WebBrowser1.AllowNavigation = True
            WebBrowser1.AllowWebBrowserDrop = True
            WebBrowser1.ScriptErrorsSuppressed = True
            System.Threading.Thread.Sleep(5000)
            WebBrowser1.Navigate("http:\\www.whatismyip.com")
    Its not Working for me.. Please Help me. Random Proxy is added correctly in Listbox. Please Help me I will Add Rep.
    How can I disable Proxy??
    Please help me.
    Thanks!!

Posting Permissions

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



Click Here to Expand Forum to Full Width