InternetReadFile

The InternetReadFile function reads data from a buffer accessible through a handle obtained by a call to InternetOpenUrl, HttpOpenRequest, or FtpOpenFile.

Note: WinINet is a high-level API that provides access to the Internet. It is suitable for many applications, but for more complex scenarios or better control, consider using the WinHTTP API.

Syntax


BOOL InternetReadFile(
  [in]      HINTERNET hFile,
  [out]     LPVOID    lpBuffer,
  [in]      DWORD     dwNumberOfBytesToRead,
  [in, out] LPDWORD   lpdwNumberOfBytesRead
);
            

Parameters

hFile

A valid HINTERNET handle for the file to be read. This handle must have been created by a call to InternetOpenUrl, HttpOpenRequest, or FtpOpenFile.

lpBuffer

A pointer to a buffer that receives the data read from the file. The buffer must be large enough to contain the data specified by dwNumberOfBytesToRead.

dwNumberOfBytesToRead

The number of bytes to read from the file.

lpdwNumberOfBytesRead

A pointer to a DWORD variable that receives the number of bytes actually read.

Return Value

Returns TRUE if the function succeeds or if the end of the file is reached. Returns FALSE if an I/O operation failed.

If the return value is TRUE and *lpdwNumberOfBytesRead is 0, the end of the file has been reached.

Tip: To retrieve extended error information, call GetLastError.

Remarks

The InternetReadFile function reads data from the specified Internet resource into the specified buffer. Data can be read in chunks of any size. If dwNumberOfBytesToRead is larger than the remaining data in the file, InternetReadFile reads all remaining data.

When downloading a file, it is recommended to allocate a buffer of a reasonable size (e.g., 4 KB) and call InternetReadFile repeatedly in a loop until the end of the file is reached.

The lpdwNumberOfBytesRead parameter indicates how many bytes were actually read into the buffer. If InternetReadFile returns TRUE and *lpdwNumberOfBytesRead is 0, it means that the end of the file has been reached.

Example

The following code example demonstrates how to read data from an HTTP resource using InternetReadFile.


#include <windows.h>
#include <wininet.h>
#include <stdio.h>

#pragma comment(lib, "wininet.lib")

int main() {
    HINTERNET hSession = NULL;
    HINTERNET hFile = NULL;
    char buffer[1024];
    DWORD bytesRead;
    BOOL success;

    // Initialize WinINet
    hSession = InternetOpen("MyHTTPClient/1.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (!hSession) {
        printf("InternetOpen failed: %lu\n", GetLastError());
        return 1;
    }

    // Open the URL
    hFile = InternetOpenUrl(hSession, "http://www.example.com", NULL, 0, 0, 0);
    if (!hFile) {
        printf("InternetOpenUrl failed: %lu\n", GetLastError());
        InternetCloseHandle(hSession);
        return 1;
    }

    // Read data from the URL
    printf("Reading data from http://www.example.com:\n");
    while (success = InternetReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead)) {
        if (bytesRead == 0) {
            break; // End of file
        }
        buffer[bytesRead] = '\0'; // Null-terminate the buffer
        printf("%s", buffer);
    }

    if (!success) {
        printf("\nInternetReadFile failed: %lu\n", GetLastError());
    }

    // Clean up
    if (hFile) {
        InternetCloseHandle(hFile);
    }
    if (hSession) {
        InternetCloseHandle(hSession);
    }

    return 0;
}
        

Requirements

Client: Windows Vista, Windows XP Professional x64 Edition, Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0 SP6, Windows 2000 Server, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016
Server: Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016
Header: declared in wininet.h
Library: use wininet.lib
DLL: use wininet.dll

See Also