Share via


Accessing URLs Directly

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

FTP and HTTP resources on the Internet can be accessed directly by using the InternetOpenUrl, InternetReadFile, and InternetFindNextFile functions. InternetOpenUrl opens 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, InternetReadFile can download it.
  2. If the resource is a directory, InternetFindNextFile can enumerate the files within the directory, except when using CERN proxies.

The InternetReadFile function is used to download resources from an HINTERNET handle returned by the InternetOpenUrl, FtpOpenFile or the HttpOpenRequest function.

InternetReadFile accepts 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 InternetReadFile in a loop to download the data and exit when it returns zero bytes read and completes successfully.

The InternetQueryDataAvailable function can be used with InternetReadFile. InternetQueryDataAvailable takes the HINTERNET handle created by InternetOpenUrl, FtpOpenFile, or HttpOpenRequest, after HttpSendRequest has 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 InternetQueryDataAvailable is 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, InternetOpenUrl can be used to access FTP directories and files. FTP requests are packaged to appear like an HTTP request, which the CERN proxy accepts.

InternetOpenUrl uses the HINTERNET handle created by the InternetOpen function and the resource URL. The URL must include a network location preceded by one of the following schemes:

  • http:
  • ftp:
  • file:
  • https:

For example, https://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 InternetOpenUrl to download the resource.

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

Aa918733.6df49998-0cf2-472e-8358-ad90eaa3eb20(en-us,MSDN.10).gif

The root HINTERNET handle created by InternetOpen is used by InternetOpenUrl. The HINTERNET handle created by InternetOpenUrl can be used by InternetQueryDataAvailable, InternetReadFile, and InternetFindNextFile (which is not pictured).

Use the InternetOpenUrl function 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 InternetOpen to initialize an Internet handle.

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

    InternetOpenUrl returns a handle that subsequent functions can use.

  3. Call InternetReadFile to download the resource file.

    InternetQueryDataAvailable can use the handle returned by InternetOpenUrl to query how much data is available to be read by a subsequent call to InternetReadFile.

  4. Call InternetCloseHandle to close the handle created by InternetOpenUrl.

  5. Call InternetCloseHandle to close the handle created by InternetOpen.

    InternetCloseHandle terminates existing activity on the handle and discards remaining data. If InternetCloseHandle closes 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