Solved

Use a proxy with webBrowser control [C#]

Posted on 2011-02-21
8
11,467 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Author Comment

by:VanHackman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, It helps in some way.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

9 Experts available now in Live!

Get 1:1 Help Now