Solved

Use a proxy with webBrowser control [C#]

Posted on 2011-02-21
8
11,603 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now