Solved

Use a proxy with webBrowser control [C#]

Posted on 2011-02-21
8
12,148 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 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
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

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 …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

627 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