WinHttpGetProxyForUrl获取数据一直为空的问题
Fxzx mic
5
信誉分
#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;
}
// 获取系统的代理设置
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) {
// 检查bypass列表
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;
}
// 设置自动代理选项
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;
// 创建一个WinHTTP会话
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;
}
// 获取代理信息
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就包含了代理服务器的地址
if (ProxyInfo.lpszProxy != NULL) {
std::wcout << L"Proxy: " << ProxyInfo.lpszProxy << std::endl;
GlobalFree(ProxyInfo.lpszProxy);
} else {
std::wcout << L"Proxy: NULL" << std::endl;
}
// 清理
if (ProxyConfig.lpszAutoConfigUrl != NULL) {
GlobalFree(ProxyConfig.lpszAutoConfigUrl);
}
WinHttpCloseHandle(hSession);
return 0;
}
你好,我有以上代码,但是不知何故,在WinHttpGetProxyForUrl
执行成功的情况下,ProxyInfo.lpszProxy
变量得到的值总是为空(NULL),请求帮助。
以下是我的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;
}
}