Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

FTP and HTTP resources on the Internet can be accessed directly by using the InternetOpenUrl, InternetReadFile, and InternetFindNextFilefunctions. InternetOpenUrlopens a connection to the resource at the URL passed to the function.

When a successful connection is established, two things can happen:

  1. If the resource is a file, InternetReadFilecan download it.

  2. If the resource is a directory, InternetFindNextFilecan enumerate the files within the directory, except when using CERN proxies.

The InternetReadFilefunction is used to download resources from an HINTERNEThandle returned by the InternetOpenUrl, FtpOpenFileor the HttpOpenRequestfunction.

InternetReadFileaccepts a void pointer variable that contains the address of a buffer and a pointer to an unsigned long integer variable that contains the buffer length. It returns the data in the buffer and the amount of data downloaded into the buffer. It returns zero bytes read and completes successfully when all available data has been read. This enables an application to use InternetReadFilein a loop to download the data and exit when it returns zero bytes read and completes successfully.

The InternetQueryDataAvailablefunction can be used with InternetReadFile. InternetQueryDataAvailabletakes the HINTERNEThandle created by InternetOpenUrl, FtpOpenFile, or HttpOpenRequest, after HttpSendRequesthas been called on the handle, and returns the number of bytes available. The application should allocate a buffer equal to the number of bytes available and use that buffer with InternetReadFile. This method does not always work because InternetQueryDataAvailableis checking the file size listed in the header and not the actual file. The data in the header file could be outdated or the header file could be missing because it is not currently required under all standards.

For applications that need to operate through a CERN proxy, InternetOpenUrlcan be used to access FTP directories and files. FTP requests are packaged to appear like an HTTP request, which the CERN proxy accepts.

InternetOpenUrluses the HINTERNEThandle created by the InternetOpenfunction and the resource URL. The URL must include a network location preceded by one of the following schemes:

  • http:

  • ftp:

  • file:

  • https:

For example, http://www.microsoft.com. The URL can also include a path, for example, /windows/functionality/, and a resource name, for example, Default.htm. For HTTP or HTTPS requests, additional headers can be included.

InternetQueryDataAvailable, InternetFindNextFile, and InternetReadFile, can use the handle created by InternetOpenUrlto download the resource.

The following illustration shows the handles to use with each function.

The root HINTERNEThandle created by InternetOpenis used by InternetOpenUrl. The HINTERNEThandle created by InternetOpenUrlcan be used by InternetQueryDataAvailable, InternetReadFile, and InternetFindNextFile(which is not pictured).

Use the InternetOpenUrlfunction to parse the URL string, establish an Internet connection, and prepare for data download. This is practical for applications that are not concerned with protocol details, but only retrieve data related to a specific URL.

To use InternetOpenUrl to access a URL
  1. Call InternetOpento initialize an Internet handle.

  2. Call InternetOpenUrlto open a connection to the URL, using the handle created by InternetOpen.

    InternetOpenUrlreturns a handle that subsequent functions can use.

  3. Call InternetReadFileto download the resource file.

    InternetQueryDataAvailablecan use the handle returned by InternetOpenUrlto query how much data is available to be read by a subsequent call to InternetReadFile.

  4. Call InternetCloseHandleto close the handle created by InternetOpenUrl.

  5. Call InternetCloseHandleto close the handle created by InternetOpen.

    InternetCloseHandleterminates existing activity on the handle and discards remaining data. If InternetCloseHandlecloses a parent handle, all child handles are also closed. If an operation requires more than one Internet handle, multiple calls may need to be made to InternetCloseHandle.

See Also

Concepts

HTTP Sessions