WinHTTP AutoProxy-Funktionen
WinHTTP implementiert das WPAD-Protokoll mithilfe der WinHttpGetProxyForUrl--Funktion zusammen mit zwei unterstützenden Hilfsfunktionen, WinHttpDetectAutoProxyConfigUrl und WinHttpGetIEProxyConfigForCurrentUser.
Die AutoProxy-Unterstützung ist nicht vollständig in den HTTP-Stapel in WinHTTP integriert. Vor dem Senden einer Anforderung muss die Anwendung WinHttpGetProxyForUrl- aufrufen, um den Namen eines Proxyservers abzurufen und dann WinHttpSetOption mithilfe von WINHTTP_OPTION_PROXY aufrufen, um die Proxykonfiguration für das WinHTTP-Anforderungshandle festzulegen, das von WinHttpOpenRequesterstellt wurde.
Die WinHttpGetProxyForUrl--Funktion kann alle drei Schritte des WPAD-Protokolls ausführen, das in der vorherigen Übersicht beschrieben ist: (1) ermitteln sie die PAC-URL, (2) laden die PAC-Skriptdatei herunter, (3) führen den Skriptcode aus und geben die Proxykonfiguration in einer WINHTTP_PROXY_INFO Struktur zurück. Wenn die Anwendung im Voraus die PAC-URL kennt, kann sie dies für WinHttpGetProxyForUrlangeben.
Im folgenden Beispielcode wird autoproxy verwendet. Sie richtet eine HTTP GET-Anforderung ein, indem zuerst die WinHTTP-Sitzungsverbindungs- und Anforderungshandles erstellt werden. Der WinHttpOpen Aufruf gibt WINHTTP_ACCESS_TYPE_NO_PROXY für die anfängliche Proxykonfiguration an, um anzugeben, dass Anforderungen standardmäßig direkt an den Zielserver gesendet werden. Mithilfe von Autoproxy wird dann die Proxykonfiguration direkt im Anforderungshandle festgelegt.
HINTERNET hHttpSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions;
WINHTTP_PROXY_INFO ProxyInfo;
DWORD cbProxyInfoSize = sizeof(ProxyInfo);
ZeroMemory( &AutoProxyOptions, sizeof(AutoProxyOptions) );
ZeroMemory( &ProxyInfo, sizeof(ProxyInfo) );
//
// Create the WinHTTP session.
//
hHttpSession = WinHttpOpen( L"WinHTTP AutoProxy Sample/1.0",
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0 );
// Exit if WinHttpOpen failed.
if( !hHttpSession )
goto Exit;
//
// Create the WinHTTP connect handle.
//
hConnect = WinHttpConnect( hHttpSession,
L"www.microsoft.com",
INTERNET_DEFAULT_HTTP_PORT,
0 );
// Exit if WinHttpConnect failed.
if( !hConnect )
goto Exit;
//
// Create the HTTP request handle.
//
hRequest = WinHttpOpenRequest( hConnect,
L"GET",
L"ms.htm",
L"HTTP/1.1",
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
0 );
// Exit if WinHttpOpenRequest failed.
if( !hRequest )
goto Exit;
//
// Set up the autoproxy call.
//
// Use auto-detection because the Proxy
// Auto-Config URL is not known.
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
// Use DHCP and DNS-based auto-detection.
AutoProxyOptions.dwAutoDetectFlags =
WINHTTP_AUTO_DETECT_TYPE_DHCP |
WINHTTP_AUTO_DETECT_TYPE_DNS_A;
// If obtaining the PAC script requires NTLM/Negotiate
// authentication, then automatically supply the client
// domain credentials.
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
//
// Call WinHttpGetProxyForUrl with our target URL. If
// auto-proxy succeeds, then set the proxy info on the
// request handle. If auto-proxy fails, ignore the error
// and attempt to send the HTTP request directly to the
// target server (using the default WINHTTP_ACCESS_TYPE_NO_PROXY
// configuration, which the requesthandle will inherit
// from the session).
//
if( WinHttpGetProxyForUrl( hHttpSession,
L"https://www.microsoft.com/ms.htm",
&AutoProxyOptions,
&ProxyInfo))
{
// A proxy configuration was found, set it on the
// request handle.
if( !WinHttpSetOption( hRequest,
WINHTTP_OPTION_PROXY,
&ProxyInfo,
cbProxyInfoSize ) )
{
// Exit if setting the proxy info failed.
goto Exit;
}
}
//
// Send the request.
//
if( !WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
WINHTTP_NO_REQUEST_DATA,
0,
0,
NULL ) )
{
// Exit if WinHttpSendRequest failed.
goto Exit;
}
//
// Wait for the response.
//
if( !WinHttpReceiveResponse( hRequest, NULL ) )
goto Exit;
//
// A response has been received, then process it.
// (omitted)
//
Exit:
//
// Clean up the WINHTTP_PROXY_INFO structure.
//
if( ProxyInfo.lpszProxy != NULL )
GlobalFree(ProxyInfo.lpszProxy);
if( ProxyInfo.lpszProxyBypass != NULL )
GlobalFree( ProxyInfo.lpszProxyBypass );
//
// Close the WinHTTP handles.
//
if( hRequest != NULL )
WinHttpCloseHandle( hRequest );
if( hConnect != NULL )
WinHttpCloseHandle( hConnect );
if( hHttpSession != NULL )
WinHttpCloseHandle( hHttpSession );
Im bereitgestellten Beispielcode weist der Aufruf von WinHttpGetProxyForUrl die Funktion an, die Proxy-Autokonfigurationsdatei automatisch zu ermitteln, indem das WINHTTP_AUTOPROXY_AUTO_DETECT Flag in der WINHTTP_AUTOPROXY_OPTIONS-Struktur angegeben wird. Die Verwendung des WINHTTP_AUTOPROXY_AUTO_DETECT-Flags erfordert, dass der Code ein oder beide der Kennzeichnungen für die automatische Erkennung (WINHTTP_AUTO_DETECT_TYPE_DHCP, WINHTTP_AUTO_DETECT_TYPE_DNS_A) angibt. Der Beispielcode verwendet das Feature für die automatische Erkennung von WinHttpGetProxyForUrl, da die PAC-URL im Voraus nicht bekannt ist. Wenn eine PAC-URL in diesem Szenario nicht im Netzwerk gespeichert werden kann, schlägt WinHttpGetProxyForUrl fehl (GetLastError- gibt ERROR_WINHTTP_AUTODETECTION_FAILEDzurück).
Wenn die PAC-URL im Voraus bekannt ist
Wenn die Anwendung die PAC-URL kennt, kann sie sie in der WINHTTP_AUTOPROXY_OPTIONS-Struktur angeben und WinHttpGetProxyForUrl- konfigurieren, um die Automatische Erkennungsphase zu überspringen.
Wenn beispielsweise eine PAC-Datei im lokalen Netzwerk unter der URL "https://InternalSite/proxy-config.pac"" verfügbar ist, würde der Aufruf von WinHttpGetProxyForUrl wie folgt aussehen.
//
// Set up the autoproxy call.
//
// The proxy auto-config URL is known. Auto-detection
// is not required.
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
// Set the proxy auto-config URL.
AutoProxyOptions. lpszAutoConfigUrl = L"https://InternalSite/proxy-config.pac";
// If obtaining the PAC script requires NTLM/Negotiate
// authentication, then automatically supply the client
// domain credentials.
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
//
// Call WinHttpGetProxyForUrl with our target URL. If auto-proxy
// succeeds, then set the proxy info on the request handle.
// If auto-proxy fails, ignore the error and attempt to send the
// HTTP request directly to the target server (using the default
// WINHTTP_ACCESS_TYPE_NO_PROXY configuration, which the request
// handle will inherit from the session).
//
if( WinHttpGetProxyForUrl( hHttpSession,
L"https://www.microsoft.com/ms.htm",
&AutoProxyOptions,
&ProxyInfo ) )
{
//...
Wenn die WINHTTP_AUTOPROXY_OPTIONS-Struktur sowohl WINHTTP_AUTOPROXY_AUTO_DETECT- als auch WINHTTP_AUTOPROXY_CONFIG_URL Flags angibt (und automatisch deskription flags und eine Autokonfigurations-URL angibt), WinHttpGetProxyForUrl erste Versuche der automatischen Erkennung und dann, wenn die automatische Erkennung eine PAC-URL nicht finden kann, "fällt zurück" auf die von der Anwendung bereitgestellte Autokonfigurations-URL.
Die WinHttpDetectAutoProxyConfigUrl-Funktion
Die WinHttpDetectAutoProxyConfigUrl--Funktion implementiert eine Teilmenge des WPAD-Protokolls: Sie versucht, die URL für die Proxy-Autokonfigurationsdatei automatisch zu erkennen, ohne die PAC-Datei herunterzuladen oder auszuführen. Diese Funktion ist in besonderen Situationen nützlich, in denen eine Webanwendung den Download und die Ausführung der PAC-Datei selbst verarbeiten muss.
Die WinHttpGetIEProxyConfigForCurrentUser-Funktion
Die WinHttpGetIEProxyConfigForCurrentUser--Funktion gibt die aktuellen Internet Explorer-Proxyeinstellungen für die aktuelle aktive Netzwerkverbindung zurück, ohne "WinInet.dll" aufzurufen. Diese Funktion ist nur nützlich, wenn sie in einem Prozess aufgerufen wird, der unter einer interaktiven Benutzerkontoidentität ausgeführt wird, da sonst wahrscheinlich keine Internet Explorer-Proxykonfiguration verfügbar ist. Beispielsweise wäre es nicht hilfreich, diese Funktion aus einer ISAPI-DLL aufzurufen, die im IIS-Dienstprozess ausgeführt wird. Weitere Informationen und ein Szenario, in dem eine WinHTTP-basierte Anwendung WinHttpGetIEProxyConfigForCurrentUserverwenden würde, finden Sie unter Discovery Without an Auto-Config File.