Use a proxy with webBrowser control C#/.net 3.5

22,118

Solution 1

Perhaps this link is useful:

http://blogs.msdn.com/b/jpsanders/archive/2011/04/26/how-to-set-the-proxy-for-the-webbrowser-control-in-net.aspx

I tested the code and it seemed to work. But two points are important:

  • It's not compatible to projects in compile mode "Any CPU" (x86 works fine)
  • JUST for HTTP proxy servers ; not for SOCKS

Solution 2

1- I guess webBrowser control checks the proxy only while its is created, so create a new control after setting the proxy

2- Navigate is not a blocking call and does not wait till page it loaded, use webBrowser.DocumentCompleted event

Below code should work (Not tested)

void Exec(string proxy,string url)
{
    var th = new Thread(() =>
    {
        SetProxy(proxy);
        using (WebBrowser wb = new WebBrowser())
        {
            wb.DocumentCompleted += (sndr, e) =>
            {
                ExecuteRoutine();
                Application.ExitThread();
            };
            wb.Navigate(url);
            Application.Run();
        }
    });
    th.SetApartmentState(ApartmentState.STA);
    th.Start();
    th.Join();
}
Share:
22,118
user1608298
Author by

user1608298

Updated on October 17, 2020

Comments

  • user1608298
    user1608298 over 3 years

    I need some help from someone who has already use the webBrowser control along with a proxys.

    What I need is the following.

    1 - Set a proxy for a webBrowser control. 2 - Load a specific site. 3 - Execute a routine over the site. 4 - Set a diferent proxy for the webBrowser control. 5 - Load another site. 6 - Execute the same routine from point number 3.

    And the process keeps in that way, looping from a list of proxys, until all of them had been used.

    But. I'm having some problems with the app. to do that:

    1 - I'm using the code attached to set the proxy into the webBrowser control, but seems to work only once during the execution, when I call it again in the loop it just doesn't work, I can get t ounderstand why.

    2 - I'm having problems to determine when the page has loaded completely, I mean, when I set the first site to load, I need the program to wait until it has finish to load, and after that execute the routine over it, and continue with the process.

    Hope some one could help me with this...

    /// The function that I'm using -----------------------------

        private void SetProxy(string Proxy)
        {
    
            MessageBox.Show("Setting :" + Proxy);
            string key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
    
            RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(key, true);
            RegKey.SetValue("ProxyServer", Proxy);
            RegKey.SetValue("ProxyEnable", 1);
    
        }
    

    // The app logic --------------------------------------

            SetProxy("190.97.219.38:80");
            webBrowser1.Navigate("http://www.whatismyip.com/");
            ExecuteRoutine();
    
            SetProxy("187.93.77.235:80");
            webBrowser1.Navigate("http://www.whatismyip.com/");
            ExecuteRoutine();
    
            SetProxy("109.235.49.243:80");
            webBrowser1.Navigate("http://www.whatismyip.com/");
            ExecuteRoutine();
    
  • Steven de Salas
    Steven de Salas over 10 years
    -1 .. And? This question and its potential solution are still relevant to programmers. As evidenced by the ~5000 views.
  • IntelliData
    IntelliData almost 9 years
    -1 To make suach an assumption is horribly wrong; The question was relevant to me as well, and I am definitely not a criminal...