Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use a proxy with webBrowser control [C#]

Posted on 2011-02-21
8
Medium Priority
?
12,346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

718 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