WinHttpGetProxyForUrl always returns ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT when I test it with the below code.
See http://msdn.microsoft.com/en-us/library/aa384122%28VS.85%29.aspx for what I have tried to base this on.
Can anyone see any flaw in my code that causes this error to always return? I have tested it with many pac files and this is always the result.
NB. you will need to set 'http://proxy.rice.edu/proxy.pac' in Internet Explorer (Tools > Internet Options > Connections tab > LAN settings > 'Use automatic configuration script' address)
Many thanks.
#include <windows.h>
#include <winhttp.h>
#include <iostream>
#pragma comment(lib, "Winhttp")
using namespace std;
int main()
{
HINTERNET httpsess = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions;
ZeroMemory(&AutoProxyOptions, sizeof(AutoProxyOptions));
WINHTTP_PROXY_INFO ProxyInfo;
DWORD ProxyInfoSize = sizeof(ProxyInfo);
ZeroMemory(&ProxyInfo, sizeof(ProxyInfo));
httpsess = WinHttpOpen(L"WinHTTP AutoProxy Sample/1.0", WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
hConnect = WinHttpConnect(httpsess, L"www.microsoft.com", INTERNET_DEFAULT_HTTP_PORT, 0);
hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/en/us/default.aspx", L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
AutoProxyOptions.lpszAutoConfigUrl = L"http://proxy.rice.edu/proxy.pac";
if (WinHttpGetProxyForUrl(httpsess, L"http://www.microsoft.com/en/us/default.aspx", &AutoProxyOptions, &ProxyInfo)) {
cout << "WinHttpGetProxyForUrl OK\n";
}
else {
DWORD dwErr = GetLastError();
switch (dwErr)
{
case ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR:
cout << "ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR\n";
break;
case ERROR_WINHTTP_INTERNAL_ERROR:
cout << "ERROR_WINHTTP_INTERNAL_ERROR\n";
break;
case ERROR_WINHTTP_OPERATION_CANCELLED:
cout << "ERROR_WINHTTP_OPERATION_CANCELLED\n";
break;
case ERROR_NOT_ENOUGH_MEMORY:
cout << "ERROR_NOT_ENOUGH_MEMORY\n";
break;
case ERROR_WINHTTP_AUTODETECTION_FAILED:
cout << "ERROR_WINHTTP_AUTODETECTION_FAILED\n";
break;
case ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT:
cout << "ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT\n";
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
cout << "ERROR_WINHTTP_INCORRECT_HANDLE_TYPE\n";
break;
case ERROR_WINHTTP_INVALID_URL:
cout << "ERROR_WINHTTP_INVALID_URL\n";
break;
case ERROR_WINHTTP_LOGIN_FAILURE:
cout << "ERROR_WINHTTP_LOGIN_FAILURE\n";
break;
case ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT:
cout << "ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT\n";
break;
case ERROR_WINHTTP_UNRECOGNIZED_SCHEME:
cout << "ERROR_WINHTTP_UNRECOGNIZED_SCHEME\n";
break;
default:
cout << "Error\n";
}
}
if (ProxyInfo.lpszProxy != NULL) GlobalFree(ProxyInfo.lpszProxy);
if (ProxyInfo.lpszProxyBypass != NULL) GlobalFree(ProxyInfo.lpszProxyBypass);
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(httpsess);
return 0;
}