Set HTTP(S) proxy programmatically

One way to assign an HTTP proxy in Windows is to directly modify the registry as follows:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"ProxyEnable"=dword:00000001
"ProxyServer"="http=127.0.0.1:2000;https=127.0.0.1:2000"
"ProxyOverride"=""
"DisablePasswordCaching"=dword:00000001
If you run this .reg file (or set the register values programmatically), the http and https proxies will be configured on port 2000.
However, I am wondering if there is a WinAPI function that can be called to configure a proxy programmatically in C++? I don't want to edit the registry directly, as this is an undocumented method. Is there a documented API for solving this problem?
Answer
That Registry setting affects the WinInet API. You can use WinInet's InternetSetOption()
function to configure the proxy settings globally or per-connection.
Enjoyed this article?
Check out more content on our blog or follow us on social media.
Browse more articles