One of the post popular ways to set Windows proxy is either via Group Policy or a script. Often these scripts apply the proxy via a registry change. However if you apply via registry change it can be difficult to get the proxy change to instantly take effect in applications, such as Internet Explorer.
Welcome our friend, InternetSetOption in WinInet.dll. Since Windows 2000 this has provided us a mechanism to set the proxy server, and also perform a “refresh” so the affect is instantly applied, with no need to refresh/restart a browser.
InternetSetOption is documented here:
For those in managed code land it can be a pain to get working exactly right, so here I provide a library that allows configuring of all proxy options available via the Internet Explorer GUI.
I’ve wrapped the library into a fully functioning command line program. (Well I hope fully functioning…)
The command line program supports the following parameters:
/proxy:[enable|disable] – Enable/Disable Proxy
/address:proxy:port – Specify Proxy Address
/pac:url – Specify PAC File URL
/autodetect:[enable|disable] – enable/disable auto detect
/connection:[connection name] – specify a dial up connection name to set proxy for
/bypasslocal – Bypass proxy for local connections.
All parameters are optional and you can use in any combination (although that may not always result in usable proxy configuration..)
So for example to enable a proxy with exceptions list:
SetProxy /proxy:enable /address:myproxy:8080 /exceptions:myintranet;*.priv;blahblah /bypasslocal
To then disable proxy:
Proxy settings should take affect immediately, with no need to relaunch applications such as Internet Explorer.
You can use quotes if parameter requires spaces or if you use <local> in your exception list:
SetProxy /proxy:enable /address:myproxy:8080 /exceptions:”<local>” /connection:”My 3G Connection”
Download the Visual Studio 2010 Project and compiled .NET 4.0 EXE here: