You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinHttpGetProxyForUrl调用成功但ProxyInfo.lpszProxy始终为NULL的问题

WinHttpGetProxyForUrl执行成功但返回lpszProxy为NULL的问题

我编写了一段C++代码,通过WinHttpGetProxyForUrl函数基于系统设置从PAC文件中获取代理服务器地址。该函数执行成功无错误,但返回的ProxyInfo.lpszProxy变量值始终为NULL。我尝试了原PAC文件和始终返回SOCKS代理的简化版PAC文件,结果均一致。请问是我的代码存在问题,还是WinHttpGetProxyForUrl函数出现了异常行为?

C++代码

#ifndef UNICODE
#define UNICODE
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0A00
#endif

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <winhttp.h>
#include <string>
#include <iostream>

#pragma comment(lib, "winhttp.lib")

int wmain(int argc, wchar_t* argv[]) {
    if (argc != 2) {
        std::wcerr << L"Parameters: <Domain>" << std::endl;
        return 1;
    }

    // Get proxy settings
    WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ProxyConfig;
    ZeroMemory(&ProxyConfig, sizeof(ProxyConfig));
    if (!WinHttpGetIEProxyConfigForCurrentUser(&ProxyConfig)) {
        std::wcerr << L"WinHttpGetIEProxyConfigForCurrentUser failed, error: "
            << GetLastError() << std::endl;
        return 1;
    }

    if (ProxyConfig.lpszProxy != NULL) {
        // Check bypass list
        if (ProxyConfig.lpszProxyBypass != NULL) {
            wchar_t* domain = wcstok(ProxyConfig.lpszProxyBypass, L";");

            while (domain != NULL) {
                if (wcscmp(domain, argv[1]) == 0) {
                    std::wcout << L"Proxy Bypass" << std::endl;
                    GlobalFree(ProxyConfig.lpszProxy);
                    GlobalFree(ProxyConfig.lpszProxyBypass);
                    return 0;
                }

                domain = wcstok(NULL, L";");
            }

            GlobalFree(ProxyConfig.lpszProxyBypass);
        }

        std::wcout << L"Proxy: " << ProxyConfig.lpszProxy << std::endl;
        GlobalFree(ProxyConfig.lpszProxy);
        return 0;
    }

    // Set AUTOPROXY options
    WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions;
    ZeroMemory(&AutoProxyOptions, sizeof(AutoProxyOptions));
    if (ProxyConfig.fAutoDetect) {
        AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
        AutoProxyOptions.dwAutoDetectFlags =
            WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
    }
    if (ProxyConfig.lpszAutoConfigUrl != NULL) {
        AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_CONFIG_URL;
        AutoProxyOptions.lpszAutoConfigUrl = ProxyConfig.lpszAutoConfigUrl;

    }
    if (AutoProxyOptions.dwFlags == NULL) {
        std::wcout << L"No Proxy" << std::endl;
        return 0;
    }
    AutoProxyOptions.fAutoLogonIfChallenged = TRUE;

    // Create WinHTTP session
    HINTERNET hSession = WinHttpOpen(
        L"Windows",
        WINHTTP_ACCESS_TYPE_NO_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS,
        0
    );
    if (hSession == NULL) {
        std::wcerr << L"WinHttpOpen failed, error: "
            << GetLastError() << std::endl;
        return 1;
    }

    // Get proxy info from PAC
    std::wstring wurl = L"http://" + (std::wstring)argv[1] + L"/";
    WINHTTP_PROXY_INFO ProxyInfo;
    ZeroMemory(&ProxyInfo, sizeof(ProxyInfo));
    if (!WinHttpGetProxyForUrl(
        hSession,
        wurl.c_str(),
        &AutoProxyOptions,
        &ProxyInfo
    )) {
        std::wcerr << L"WinHttpGetProxyForUrl failed, error: "
            << GetLastError() << std::endl;
        WinHttpCloseHandle(hSession);
        return 1;
    }

    // ProxyInfo.lpszProxy contains the address of the proxy server
    if (ProxyInfo.lpszProxy != NULL) {
        std::wcout << L"Proxy: " << ProxyInfo.lpszProxy << std::endl;
        GlobalFree(ProxyInfo.lpszProxy);
    } else {
        std::wcout << L"Proxy: NULL" << std::endl;
    }

    // Cleanup
    if (ProxyConfig.lpszAutoConfigUrl != NULL) {
        GlobalFree(ProxyConfig.lpszAutoConfigUrl);
    }

    WinHttpCloseHandle(hSession);

    return 0;
}

原PAC文件内容

var pHost = "Proxy.local";
var pPort = "1080";
var Socks5 = "SOCKS5 " + pHost + ":" + pPort + "; ";
var Socks = "SOCKS " + pHost + ":" + pPort + "; ";
var Direct = "DIRECT; ";

function FindProxyForURL(url, host) {
        if (shExpMatch(dnsResolveEx(host), "*.*.*.*")) {
                return Socks5 + Socks + Direct;
        } else {
                return Direct;
        }
}

简化版PAC文件内容

function FindProxyForURL(url, host) {
    return "SOCKS Proxy.local:1080";
}

内容的提问来源于stack exchange,提问作者Fxzx mic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 23:54:59