Solved

Use a proxy with webBrowser control [C#]

Posted on 2011-02-21
8
11,793 Views
Last Modified: 2013-12-17
Hello,

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

...

Open in new window

0
Comment
Question by:VanHackman
  • 4
  • 3
8 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34946521
I have little experience with the WebBrowser, and no experience with proxys - however, knowing that the WebBrowser control just starts an instance of iexplore.exe I can guess that iexplore.exe only reads the proxy values once at startup.  Since you're never stopping/re-starting iexplore.exe it never reads the new values.

You will probably need to manually set webBrowser1 = new WebBrowser() (and possibly destroy the old one first with a call to Dispose() and/or System.Runtime.InteropServices.Marshal.ReleaseComObject http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx).

Also have a look at the Windows API InternetSetOption (http://msdn.microsoft.com/en-us/library/aa385114(VS.85).aspx)
0
 
LVL 11

Author Comment

by:VanHackman
ID: 34946777
Hi,

But I'm not sure about how to use InternetSetOption to set a proxy into the webBrowser control, seems like if works with an axWebBrowser instead of a simple webBrowser control.

Also, Could you provide me an example of how to set up a webBrowser control directly in the code, and how to relase it after each iteration in the loop?

Thanks in advance, I really need help getting this to work. =/
0
 
LVL 10

Accepted Solution

by:
eguilherme earned 500 total points
ID: 34946826
well my first suggestion since what you are trying to do is automate a process in a web page is: abandon webbrowser, its slow and bad choice for web automation..

read about the WebClient / HttpWebRequest classes, both allow you to tell the proxy (WebProxy) and you will always know when the page has finished loading.. the only issue is that you must know how to page work (cookies/etc) to simulate the same thing..

but after you learn that, you will be able to automate any html page you want..
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34946843
Assuming the web browser is the only thing on the form...then leave the form empty:

(I didn't test this, so not entirely sure it'll work as expected)

SetProxy("190.97.219.38:80");
WebBrowser wb = new WebBrowser();
wb.DockStyle = DockStyle.Fill;
this.Controls.Add(wb);
wb.Navigate("http://www.whatismyip.com/");
ExecuteRoutine();
this.Controls.Remove(wb);
wb.Dispose()

SetProxy("187.93.77.235:80");
WebBrowser wb = new WebBrowser();
wb.DockStyle = DockStyle.Fill;
this.Controls.Add(wb);
wb.Navigate("http://www.whatismyip.com/");
ExecuteRoutine();
this.Controls.Remove(wb);
wb.Dispose()

Open in new window

0
 
LVL 11

Author Comment

by:VanHackman
ID: 34946858
I will not using cookies, just need to perform a little proccess in each page.

Do you think that some code as the one below could work with WebClient or HttpWebRequest class?


// Click a button ------------------------------

        private void ClickButton(string tag, string attribute, string attName)
        {
            HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName(tag);

            foreach (HtmlElement element in col)
            {

                if (element.GetAttribute(attribute).Equals(attName))
                {

                    element.InvokeMember("click");
                }
            }
        }

// Fill up a input ---------------------------------

        private void SetText(string tag, string attribute, string attName, string value)
        {

            // Get a collection of all the tags with name "input";

            HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName(tag);

            foreach (HtmlElement currentTag in tagsCollection)
            {

                // If the attribute of the current tag has the name attName

                if (currentTag.GetAttribute(attribute).Equals(attName))

                    // Then set its attribute "value".

                    currentTag.SetAttribute("value", value);
            }
        }

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34946892
It depends on what you're trying to do.  If all you want is to open www.whatismyip.com and read what it thinks your IP address is, that could be done pretty easily with a WebClient/HttpRequest.

But I see you're emulating the click of a button, and depending on what that button does, it might get tricky to duplicate with a WebClient/HttpRequest.  For example, if that button runs a JavaScript function on the page you might have trouble - the WebClient and HttpRequest (to my knowledge) don't have any support JavaScript.
0
 
LVL 11

Author Comment

by:VanHackman
ID: 34946966
Ok, I will explain this.

What i'm trying to do is a C# app. that allows me to recommend sites to a whole list of people using the "addthis" "Email a Friend" form.

(http://www.addthis.com/)

I mean, the app. will open a page like:

http://api.addthis.com/oexchange/0.8/forward/email/offer?url=http://www.jcpenney.com/jcp/default.aspx&title=JCPenney&username=AddThis&pco=crx-200

Fill up the "to", "from"  and "note" inputs, and submit the form.

For the for the "to" and "note" are text areas, "from" is a input field, and "Send" is a simple HTML button.

So, which class or control do you think will help me the best in this case?
0
 
LVL 11

Author Closing Comment

by:VanHackman
ID: 34983790
Thanks, It helps in some way.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question