Brought to you by the |
The following table summarizes the Win32 Internet functions. It uses indenting to show the dependencies among functions. A dependent function can be called only after the related higher-level function is called. This is because the higher-level function returns a handle and sets up a state at the protocol level that is a prerequisite to the successful execution of the dependent function or functions.
InternetOpen | Initializes the application's use of the Win32 Internet functions. |
InternetOpenUrl | Begins retrieving an FTP, Gopher, or HTTP URL. |
InternetReadFile | Reads URL data. |
InternetSetFilePointer | Sets the position for the next read in a file. |
InternetCloseHandle | Stops reading data from the URL. |
InternetSetStatusCallback | Sets a callback function that is called with status information. |
InternetQueryOption | Queries the setting of an Internet option. |
InternetSetOption | Sets an Internet option. |
InternetCrackUrl | Parses a URL string into components. |
InternetCreateUrl | Creates a URL string from components. |
InternetCanonicalizeUrl | Converts a URL to a canonical form. |
InternetCombineUrl | Combines base and relative URLs. |
InternetErrorDlg | Displays predefined dialog boxes for common Internet error conditions. |
InternetConfirmZoneCrossing | Checks for changes between secure and non-secure URLs. |
InternetTimeFromSystemTime | Formats a date and time according to the specified RFC format (as specified in the HTTP version 1.0 specification). |
InternetTimeToSystemTime | Takes an HTTP time/date string and converts it to a SYSTEMTIME structure. |
InternetAttemptConnect | Allows an application to attempt to connect to the Internet before issuing any requests. |
FTP | |
InternetConnect | Opens an FTP session with a server and logs on the user. |
FtpFindFirstFile | Starts file enumeration or file search in the current directory. |
InternetFindNextFile | Continues file enumeration or search. |
InternetCloseHandle | Ends directory file enumeration or search. |
FtpGetFile | Retrieves an entire file from the server. |
FtpPutFile | Writes an entire file to the server. |
FtpDeleteFile | Deletes a file on the server. |
FtpRenameFile | Renames a file on the server. |
FtpOpenFile | Initiates access to a file on the server for either reading or writing. |
InternetQueryDataAvailable | Queries the amount of data available |
InternetReadFile | Reads data from an open file. |
InternetWriteFile | Writes data to an open file. |
InternetCloseHandle | Ends a read or write operation from or to a file on the server. |
FtpCreateDirectory | Creates a new directory on the server. |
FtpRemoveDirectory | Deletes a directory on the server. |
FtpSetCurrentDirectory | Changes the client's current directory on the server. |
FtpGetCurrentDirectory | Returns the client's current directory on the server. |
InternetGetLastResponseInfo | Retrieves the text of the server's response to the FTP command. |
InternetCloseHandle | Closes the FTP session. |
Gopher | |
InternetConnect | Indicates the Gopher server that the application is interested in accessing. |
GopherFindFirstFile | Starts enumerating a Gopher directory listing. |
InternetFindNextFile | Continues enumerating a Gopher directory listing. |
InternetCloseHandle | Stops enumerating a Gopher directory listing. |
GopherOpenFile | Starts retrieving a Gopher object. |
InternetQueryDataAvailable | Queries the amount of data available. |
InternetReadFile | Reads data from a Gopher object. |
InternetCloseHandle | Completes the reading of a Gopher object. |
GopherCreateLocator | Forms a Gopher locator for use in other Gopher function calls. |
GopherGetAttribute | Retrieves attribute information on the Gopher object. |
InternetCloseHandle | Indicates that the application is no longer interested in the server. |
HTTP (World Wide Web) | |
InternetConnect | Indicates the HTTP server the application is interested in accessing. |
HttpOpenRequest | Opens an HTTP request handle. |
HttpAddRequestHeaders | Adds HTTP request headers to the HTTP request handle. |
HttpSendRequest | Sends the specified request to the HTTP server. |
InternetQueryDataAvailable | Queries the amount of data available |
InternetReadFile | Reads a block of data from an outstanding HTTP request. |
HttpQueryInfo | Queries information about an HTTP request. |
InternetCloseHandle | Completes the application's use of the Win32 Internet functions. |
InternetCloseHandle | Completes the application's use of the Win32 Internet functions. |
The handles that are created and used by the Win32 Internet functions are opaque handle types called HINTERNETS. These handles returned by the Win32 Internet function APIs are not interchangeable with the base Win32 handles, so they cannot be used with Win32 APIs such as ReadFile or CloseHandle. Similarly, base Win32 handles cannot be used with the Win32 Internet function APIs. For example, a handle returned by CreateFile cannot be passed to InternetReadFile.
If a callback function was registered for a handle, all operations on that handle can generate status indications, provided that the context value that was supplied when the handle was created was not zero. Providing a zero context value is a method to force an operation to complete synchronously, even though INTERENT_FLAG_ASYNC was specified in InternetOpen.
Status indications are mainly intended to give the application feedback as to the progress of an operation, and are mainly concerned with network operations, such as resolving a host name, connecting to a server, and receiving data. Three special-purpose status indications can be made for a handle:
The application must check the INTERNET_ASYNC_RESULTS structure to determine whether the operation succeeded or failed after receiving an INTERNET_STATUS_REQUEST_COMPLETE indication.
The InternetCloseHandle function closes handles of type HINTERNET and all handles that descended from it in the Handle Hierarchy function. Note that handle values are recycled quickly; therefore, if a handle is closed and a new handle is generated immediately, there is a good chance that the new handle will have the same value as the handle just closed.
Handles returned from Win32 Internet functions are maintained in a tree hierarchy. The handle returned by the InternetOpen function is the root node. Handles returned from the InternetConnect function occupy the next level. Currently, handles that are returned by open or find functions, such as HttpOpenRequest and FtpFindFirstFile, are the leaf nodes. This structure can be used by InternetCloseHandle to close a single handle or an entire subtree.
The Win32 Internet functions are "reentrant" in the sense that there can be multiple calls to an individual function from different threads. The functions complete any necessary synchronization. However, multiple simultaneous calls using the same Internet connection can lead to unpredictable results.
For example, if an application has used FtpOpenFile to begin downloading a file from an FTP server, and two threads simultaneously make calls to InternetReadFile, there is no guarantee which call will be completed first, or which thread will receive file data first. Applications that use multiple threads for the same Internet connection are responsible for synchronization between threads to ensure a predictable return of information.
The Win32 Internet functions return error information in the same way as Win32 functions. Return values tell whether the function is successful or not. For example, some Internet functions return a BOOL value that is TRUE if the function succeeded or FALSE if it failed, and others return a handle of type HINTERNET. A NULL handle indicates that the function failed, and any other value indicates that it succeeded.
If a function fails, the application can call the Win32 Internet function GetLastError to retrieve the specific error code for the failure. In addition, the FTP and Gopher protocols let servers return additional error information. For these protocols, applications can use the InternetGetLastResponseInfo function to retrieve error text.
Both GetLastError and InternetGetLastResponseInfo operate on a per-thread basis. If two threads call Internet functions at the same time, error information will be returned for each of the individual threads so that there is no conflict between the threads.
The Win32 Internet functions do not currently provide support for Unicode. However, support will be provided in future versions.
Many of the Win32 Internet functions accept a double-word array of flags as a parameter. The following is a brief description of the defined flags:
Many of the Win32 Internet functions that create a handle can also accept an application-defined context value. This context value is associated with the handle until it is closed. For example, you can specify a context value to the HttpOpenRequest function that will be used in all callbacks made for requests against this handle. If the INTERNET_FLAG_ASYNC flag is specified, supplying a zero context value forces the request to be synchronous.
By default, the Win32 Internet functions operate synchronously. An application can request asynchronous operation by setting the INTERNET_FLAG_ASYNC flag in the call to the InternetOpen function. All future calls made against handles derived from the handle returned from InternetOpen will be made asynchronously.
The rationale for asynchronous versus synchronous operation is to allow a single-threaded application to maximize its utilization of the CPU without having to wait for network I/O to complete. Therefore, depending on the request, the operation may complete synchronously or asynchronously. The application should check the return code. If a function returns FALSE or NULL, and GetLastError returns ERROR_IO_PENDING, the request has been made asynchronously, and the application will be called back with INTERNET_STATUS_REQUEST_COMPLETE when the function has completed.
For an application to be able to make requests asynchronously, it must set the INTERNET_FLAG_ASYNC flag in the call to InternetOpen, it must register a valid callback function, and it must supply a non-zero context value.
Win32 Internet functions have built-in caching support that is simple yet flexible. Any data that is retrieved from the network is cached on the hard disk and retrieved for subsequent requests. The caller has the option of controlling the caching on a per-request basis. In the case of HTTP, most headers received from the server are also cached. When an HTTP request is satisfied from the cache, the cached headers are also returned to the caller. This makes data download from Win32 Internet functions seamless, whether it is coming from the cache or from the wire.
For the APIs that return strings, there is an input lpszBuffer parameter and an lpdwBufferLength parameter. The lpszBuffer can be NULL, and lpdwBufferLength must be a valid pointer to a DWORD variable. If the input buffer pointed to by lpszBuffer is either too small to hold the output string or NULL, a failure indication will be returned by the API and GetLastError will return ERROR_INSUFFICIENT_BUFFER. The variable pointed to by lpdwBufferLength will contain a number that represents the number of bytes that are required by the function to return the requested string, which includes the NUL terminator. The application should allocate a buffer of this size, set the variable pointed to by lpdwBufferLength to this value, and resubmit the request. If the size of the buffer is sufficient to receive the requested string, the string is copied to the output buffer with a NUL terminator and a success indication is returned by the API. The variable pointed to by lpdwBufferLength will now contain the number of characters stored in the buffer, excluding the NUL terminator.