views:

191

answers:

1

I'm writing a small C# application that will use Internet Explorer to interact with a couple a websites, with help from WatiN.

However, it will also require from time to time to use a proxy.

I've came across Programmatically Set Browser Proxy Settings in C#, but this only enables me to enter a proxy address, and I also need to enter a Proxy username and password. How can I do that?

Note:

  • It doesn't matter if a solution changes the entire system Internet settings. However, I would prefer to change only IE proxy settings (for any connection).
  • The solution has to work with IE8 and Windows XP SP3 or higher.
  • I'd like to have the possibility to read the Proxy settings first, so that later I can undo my changes.

EDIT

With the help of the Windows Registry accessible through Microsoft.Win32.RegistryKey, i was able to apply a proxy something like this:

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");

But how can i specify a username and a password to login at the proxy server?

I also noticed that IE doesn't refresh the Proxy details for its connections once the registry was changed how can i order IE to refresh its connection settings from the registry?

Thanks

A: 

Here's how I've done it when accessing a web service through a proxy:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);

        WebProxy proxy = new WebProxy(proxyUrl, port);

        NetworkCredential nwCred = new NetworkCredential(proxyUser, proxyPassword);

        CredentialCache credCache = new CredentialCache();
        credCache.Add(proxy.Address, "Basic", nwCred);
        credCache.Add(proxy.Address, "Digest", nwCred);
        credCache.Add(proxy.Address, "Negotiate", nwCred);
        credCache.Add(proxy.Address, "NTLM", nwCred);
        proxy.Credentials = credCache;

        proxy.BypassProxyOnLocal = false;

        request.Proxy = proxy;
ebpower
@ebpower: I don't see how that would help me. Note that my application doesn't browse/request anything from the web, only commands IE to do it, that why i need to set a proxy either to IE or to the system. But thanks any way, the next version of my application will surely need to perform some Http requests, this will come in handy.
Fábio Antunes
@fabio: Oops - sorry, I didn't read the question closely enough. I know of no setting from IE's internet options to set username and password. Aren't these provided automatically by the login user's credentials?
ebpower
@ebpower: Lol, you still didn't get it. I need my C# app to apply a Proxy to Internet Explorer / Operative System.
Fábio Antunes