Fábio Antunes
Fábio Antunes

Reputation: 17164

Programmatically Set Proxy Address, Port, User, Password throught Windows Registry

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:

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

Upvotes: 5

Views: 8438

Answers (2)

Mike L.
Mike L.

Reputation: 1966

For IE, you can use the same place in the registry. Just set ProxyServer="user:[email protected]:8080" however firefox completely rejects this, and does not attempt to connect.

Upvotes: 2

Ed Power
Ed Power

Reputation: 8531

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;

Upvotes: 1

Related Questions