|
WinHttpOpen |
|
Description
The WinHttpOpen function initializes, for an application, the use of WinHTTP functions and returns a WinHTTP-session handle.
C/C++ Syntax
PowerBASIC Syntax
Parameters
pwszUserAgent
[in] A pointer to a null-terminated Unicode string that contains the name of the application or entity calling the WinHTTP functions. This name is used as the user agent in the HTTP protocol.
dwAccessType
[in] Type of access required. This can be one of the following values.
WINHTTP_ACCESS_TYPE_NO_PROXY
Resolves all host names locally.
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
Retrieves the static proxy or direct configuration from the registry. WINHTTP_ACCESS_TYPE_DEFAULT_PROXY does not inherit browser proxy settings. WinHTTP does not share any proxy settings with Internet Explorer. This option picks up the WinHTTP proxy configuration set by the WinHTTP Proxycfg.exe utility.
WINHTTP_ACCESS_TYPE_NAMED_PROXY
Passes requests to the proxy unless a proxy bypass list is supplied and the name to be resolved bypasses the proxy. In this case, this function uses WINHTTP_ACCESS_TYPE_NAMED_PROXY.
pwszProxyName
[in] A pointer to a null-terminated Unicode string that contains the name of the proxy server to use when proxy access is specified by setting dwAccessType to WINHTTP_ACCESS_TYPE_NAMED_PROXY. The WinHTTP functions recognize only CERN type proxies for HTTP. If dwAccessType is not set to WINHTTP_ACCESS_TYPE_NAMED_PROXY, this parameter must be set to WINHTTP_NO_PROXY_NAME.
pwszProxyBypass
[in] A pointer to a null-terminated Unicode string that contains an optional list of host names or IP addresses, or both, that should not be routed through the proxy when dwAccessType is set to WINHTTP_ACCESS_TYPE_NAMED_PROXY. The list can contain wildcard characters. Do not use an empty string, because the WinHttpOpen function uses it as the proxy bypass list. If this parameter specifies the "<local>" macro as the only entry, this function bypasses any host name that does not contain a period. If dwAccessType is not set to WINHTTP_ACCESS_TYPE_NAMED_PROXY, this parameter must be set to WINHTTP_NO_PROXY_BYPASS.
dwFlags
[in] Unsigned long integer value that contains the flags that indicate various options affecting the behavior of this function. This parameter can have the following value.
WINHTTP_FLAG_ASYNC
Use the WinHTTP functions asynchronously. By default, all WinHTTP functions that use the returned HINTERNET handle are performed synchronously.
Return Value
Returns a valid session handle if successful, or NULL otherwise. To retrieve extended error information, call GetLastError. Among the error codes returned are:
Remarks
Even when WinHTTP is used in asynchronous mode (that is, when WINHTTP_FLAG_ASYNC has been set in WinHttpOpen), this function operates synchronously. The return value indicates success or failure. To retrieve extended error information, call GetLastError.
The WinHttpOpen function is the first of the WinHTTP functions called by an application. It initializes internal WinHTTP data structures and prepares for future calls from the application. When the application finishes using the WinHTTP functions, it must call WinHttpCloseHandle to free the session handle and any associated resources.
The application can make any number of calls to WinHttpOpen, though a single call is normally sufficient. Each call to WinHttpOpen opens a new session context. Because user data is not shared between multiple session contexts, an application that makes requests on behalf of multiple users should create a separate session for each user, so as not to share user-specific cookies and authentication state. The application should define separate behaviors for each WinHttpOpen instance, such as different proxy servers configured for each.
After the calling application has finished using the HINTERNET handle returned by WinHttpOpen, it must be closed using the WinHttpCloseHandle function.
Example Code [PowerBASIC]
The following example code shows how to retrieve the default connection time-out value.
#COMPILE EXE #DIM ALL #INCLUDE "WIN32API.INC" #INCLUDE "WINHTTP.INC"
FUNCTION PBMAIN () AS LONG
LOCAL hSession AS DWORD LOCAL pData AS DWORD LOCAL dwSize AS DWORD LOCAL strUserAgent AS STRING
dwSize = 4 ' size of a DWORD
' Use WinHttpOpen to obtain a session handle. strUserAgent = UCODE$("A WinHTTP Example Program/1.0" & $NUL) hSession = WinHttpOpen(STRPTR(strUserAgent), _ %WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, _ %WINHTTP_NO_PROXY_NAME, _ %WINHTTP_NO_PROXY_BYPASS, _ 0)
IF hSession THEN ' Use WinHttpQueryOption to retrieve internet options. IF WinHttpQueryOption(hSession, _ %WINHTTP_OPTION_CONNECT_TIMEOUT, _ VARPTR(pdata), _ dwSize) THEN PRINT "Connection timeout: " pdata ELSE PRINT "Error " GetLastError " in WinHttpQueryOption" END IF ' When finished, release the HINTERNET handle. WinHttpCloseHandle(hSession) ELSE PRINT "Error " GetLastError " in in WinHttpOpen." END IF
' Close open handles. IF hSession THEN WinHttpCloseHandle(hSession)
WAITKEY$
END FUNCTION
Example Code [C++]
DWORD data; DWORD dwSize = sizeof(DWORD);
// Use WinHttpOpen to obtain an HINTERNET handle. HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) {
// Use WinHttpQueryOption to retrieve internet options. if (WinHttpQueryOption( hSession, WINHTTP_OPTION_CONNECT_TIMEOUT, &data, &dwSize)) { printf("Connection timeout: %u ms\n\n",data); } else { printf( "Error %u in WinHttpQueryOption.\n", GetLastError()); }
// When finished, release the HINTERNET handle. WinHttpCloseHandle(hSession); } else { printf("Error %u in WinHttpOpen.\n", GetLastError()); }
|
