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

2019-02-09 20:52发布

问题:

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();

回答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


回答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();
}


回答3:

I had a somewhat similar question in the past. The accepted answer for the question suggests to take a look at this Microsoft Knowledge Base article:

"How to programmatically query and set proxy settings under Internet Explorer"

Basically, you have to do some P/Invoke and call some WinInet DLL functions. Although I never tried it in a real-world project, I strongly assume that this is the way to go.



回答4:

Just to let you all know, this guy has posted 5 question, all asking the same thing, and based on his first question and how badly he was knocked down, it seems he is trying to commit some type of cybercrime. Now, based on my reading of his intellect, he'll probably end up in prison really quickly, but I'm just thinking perhaps we can save him from that by letting him know that it's not possible to provide an imaginary IP address to services you are communicating with (since if you did, the service will not be able to reach you to provide a response). Here is his entertaining list:

https://stackoverflow.com/questions/12045317/how-to-hide-my-ip-address-c-net-3-5

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

how to pass ip-address to webBrowser control

how to use custom ip address to browse a web page c#/.net

https://stackoverflow.com/questions/12019890/how-to-load-webpage-using-user-provided-ipaddress-webbrowser-control-c-net

And now, I think he has created a new username, user1563019, with more proxy/settings questions below: https://stackoverflow.com/users/1563019/user1563019