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 Browse开发者_C百科r 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
For IE, you can use the same place in the registry. Just set ProxyServer="user:password@127.0.0.1:8080" however firefox completely rejects this, and does not attempt to connect.
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;
精彩评论