Brought to you by the
RFC INDEX PAGE
Google
 


Microsoft Win32 Internet Programmer's Reference


Overview of the Win32 Internet Functions

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.

Handles

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.

Handle Hierarchy

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.

Multithreaded Access

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.

Error Handling

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.

Unicode Support

The Win32 Internet functions do not currently provide support for Unicode. However, support will be provided in future versions.

API Flags

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:

INTERNET_FLAG_RELOAD
Force a download of the requested file, object, or directory listing from the origin server, not from the cache. The GopherFindFirstFile, GopherOpenFile, FtpFindFirstFile, FtpGetFile, FtpOpenFile, FtpPutFile, HttpOpenRequest, and InternetOpenUrl functions utilize this flag.
INTERNET_FLAG_RAW_DATA
Return the data as a GOPHER_FIND_DATA structure when retrieving Gopher directory information, or as a WIN32_FIND_DATA structure when retrieving FTP directory information using the InternetOpenUrl API.
INTERNET_FLAG_EXISTING_CONNECT
Attempt to use an existing InternetConnect object if one exists with the same attributes required to make the request. Only the InternetOpenUrl function uses this flag.
INTERNET_FLAG_ASYNC
Make only asynchronous requests on handles descended from the handle returned from this function. Only the InternetOpen function uses this flag.
INTERNET_FLAG_PASSIVE
Use passive FTP semantics. Only InternetConnect and InternetOpenUrl use this flag. InternetConnect uses this flag for FTP requests, and InternetOpenUrl uses this flag for FTP files and directories.
INTERNET_FLAG_NO_CACHE_WRITE
Do not add the returned entity to the cache. This flag is used by GopherFindFirstFile, GopherOpenFile, FtpFindFirstFile, FtpGetFile, FtpPutFile, HttpOpenRequest, and InternetOpenUrl.
INTERNET_FLAG_MAKE_PERSISTENT
Add the returned entity to the cache as a persistent entity. This means that standard cache cleanup, consistency checking, or garbage collection cannot remove this item from the cache. This flag is used by GopherFindFirstFile, GopherOpenFile, FtpFindFirstFile, FtpGetFile, FtpOpenFile, FtpPutFile, HttpOpenRequest, and InternetOpenUrl.
INTERNET_FLAG_OFFLINE
Do not make network requests. All entities are returned from the cache. If the requested item is not in the cache, a suitable error, such as ERROR_FILE_NOT_FOUND, is returned. Only the InternetOpen function uses this flag.
INTERNET_FLAG_SECURE
Use secure transaction semantics. This translates to using Secure Sockets Layer/Private Communications Technology (SSL/PCT) and is only meaningful in HTTP requests. This flag is used by HttpOpenRequest and InternetOpenUrl, but this is made redundant if "https://" appears in the URL.
INTERNET_FLAG_KEEP_CONNECTION
Use keep-alive semantics, if available, for the connection. This flag is used by HttpOpenRequest and InternetOpenUrl (for HTTP requests).
INTERNET_FLAG_NO_AUTO_REDIRECT
Do not automatically handle redirection in HttpSendRequest. This flag can also be used by InternetOpenUrl for HTTP requests.
INTERNET_FLAG_IGNORE_CERT_CN_INVALID
Disable Win32 Internet function checking of SSL/PCT-based certificates that are returned from the server against the host name given in the request. Win32 Internet functions use a simple check against certificates by comparing for matching host names and simple wildcarding rules. This flag can be used by HttpOpenRequest and InternetOpenUrl (for HTTP requests).
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
Disable Win32 Internet function checking of SSL/PCT-based certificates for proper validity dates. This flag can be used by HttpOpenRequest and InternetOpenUrl (for HTTP requests).
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS
Disable the ability of the Win32 Internet functions to detect this special type of redirect. When this flag is used, Win32 Internet functions transparently allow redirects from HTTP to HTTPS URLs. This flag can be used by HttpOpenRequest and InternetOpenUrl (for HTTP requests).
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP
Disable the ability of the Win32 Internet functions to detect this special type of redirect. When this flag is used, Win32 Internet functions transparently allow redirects from HTTPS to HTTP URLs. This flag can be used by HttpOpenRequest and InternetOpenUrl (for HTTP requests).
INTERNET_FLAG_READ_PREFETCH
Download the entity without requiring the application to initiate reads from the network. This flag can be used by HttpOpenRequest and InternetOpenUrl (for HTTP requests).
INTERNET_FLAG_NO_COOKIES
Do not automatically add cookie headers to requests, and do not automatically add returned cookies to the cookie database. This flag can be used by HttpOpenRequest and InternetOpenUrl (for HTTP requests).
INTERNET_FLAG_NO_AUTH
Do not attempt authentication automatically. This flag can be used by HttpOpenRequest and InternetOpenUrl (for HTTP requests).
INTERNET_FLAG_TRANSFER_ASCII
Transfer file as ASCII (FTP only). This flag can be used by FtpOpenFile, FtpGetFile, and FtpPutFile.
INTERNET_FLAG_TRANSFER_BINARY
Transfer file as binary (FTP only). This flag can be used by FtpOpenFile, FtpGetFile, and FtpPutFile.
INTERNET_FLAG_MUST_CACHE_REQUEST
Cause operation to fail if the downloaded file cannot be cached. This flag can be used by GopherFindFirstFile, GopherOpenFile, FtpFindFirstFile, FtpGetFile, FtpOpenFile, FtpPutFile, HttpOpenRequest, and InternetOpenUrl.
INTERNET_FLAG_RESYNCHRONIZE
Perform a conditional download of the file. This flag can be used by GopherFindFirstFile, GopherOpenFile, FtpFindFirstFile, FtpGetFile, FtpOpenFile, FtpPutFile, HttpOpenRequest, and InternetOpenUrl.
INTERNET_FLAG_HYPERLINK
Force a reload if there was no Expires time and no Last-Modified time returned from the server when determining whether to reload the item from the network. This flag can be used by GopherFindFirstFile, GopherOpenFile, FtpFindFirstFile, FtpGetFile, FtpOpenFile, FtpPutFile, HttpOpenRequest, and InternetOpenUrl.

Context Values

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.

Asynchronous Support

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.

Persistent Caching

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.

Buffers and Buffer Parameters

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.

© 1996 Microsoft Corporation