Using Network Proxies with VB6: Configuration and Code Examples
Overview
Network proxies in VB6 let your application route HTTP/FTP requests through intermediary servers for caching, filtering, or network policy compliance. In VB6 you typically use WinInet, WinHTTP, or third‑party libraries (MSXML2, .NET interop) to make network requests and configure proxy settings.
Methods (short)
- WinInet (InternetOpen/InternetConnect/HttpOpenRequest): respects per‑user Internet Explorer proxy settings; easy for simple HTTP.
- WinHTTP: suitable for services/desktop apps needing explicit proxy control; supports programmatic proxy configuration.
- MSXML2.XMLHTTP / ServerXMLHTTP: COM-based; XMLHTTP uses WinInet (IE settings), ServerXMLHTTP uses WinHTTP (explicit proxy control).
- Third‑party controls / .NET interop: when modern TLS or advanced features are required.
Configuration approaches
- Use system/IE settings (default for WinInet and MSXML2.XMLHTTP).
- Set proxy explicitly in code (WinHTTP, ServerXMLHTTP, or WinInet with InternetSetOption).
- Provide proxy credentials when required (Basic/NTLM).
Example 1 — MSXML2.XMLHTTP (uses IE proxy settings)
vb
Dim xhr As Object Set xhr = CreateObject(“MSXML2.XMLHTTP”) xhr.Open “GET”, “http://example.com”, False xhr.Send If xhr.Status = 200 ThenMsgBox xhr.responseText ElseMsgBox "HTTP error: " & xhr.StatusEnd If Set xhr = Nothing
Example 2 — MSXML2.ServerXMLHTTP (explicit proxy + auth)
vb
Dim req As Object Set req = CreateObject(“MSXML2.ServerXMLHTTP.6.0”) ‘ Set proxy: “http=proxyhost:8080;https=proxyhost:8080” or use SXH_PROXY_SETDEFAULT req.setProxy 2, “http=proxy.example.com:8080;https=proxy.example.com:8080” ’ If proxy requires credentials: req.setProxyCredentials “DOMAIN\username”, “password” req.open “GET”, “http://example.com”, False req.send MsgBox req.status & “ - ” & Left(req.responseText, 200) Set req = NothingNotes: setProxy constants — 0 = default, 1 = no proxy, 2 = named proxy.
Example 3 — WinHTTP via WinHTTP COM (explicit control)
vb
Dim winReq As Object Set winReq = CreateObject(“WinHttp.WinHttpRequest.5.1”) winReq.SetProxy 2, “proxy.example.com:8080” winReq.Open “GET”, “http://example.com”, False ‘ For proxy auth: winReq.SetCredentials “username”, “password”, 0 winReq.Send MsgBox winReq.Status & “ - ” & Left(winReq.ResponseText, 200) Set winReq = NothingExample 4 — WinInet (advanced; respects IE settings; can override)
- Use InternetOpen with INTERNET_OPEN_TYPE_PROXY and InternetSetOption to set INTERNET_OPTION_PROXY.
- Requires Declare statements and structure handling (not shown here to keep concise).
Proxy Authentication
- For Basic auth, include credentials via SetCredentials / SetProxyCredentials or set Authorization header.
- For NTLM/Kerberos, prefer WinHTTP/ServerXMLHTTP and ensure correct flags/credentials (may require running under appropriate user context).
Troubleshooting
- Confirm which stack your code uses (WinInet vs WinHTTP) — behavior differs.
- Check IE/system proxy vs explicit proxy calls; mismatches cause failures.
- Use Fiddler or Wireshark to inspect requests.
- Handle timeouts and HTTP error codes; enable asynchronous calls for responsiveness.
Compatibility & TLS
- VB6-era stacks may not support modern TLS versions by default. Use updated WinHTTP/WinInet on modern Windows or use .NET interop / external libraries for current TLS ciphers.
If you want, I can provide the WinInet declare example or a ready VB6 module that wraps proxy configuration and HTTP GET/POST routines.
Leave a Reply