WinINet Functions
The WinINet API provides high-level access to the Internet for client applications. It handles the complexities of protocols like HTTP, FTP, and Gopher.
Commonly Used Functions
-
InternetOpen
Initializes an application's use of the WinINet API.
-
InternetConnect
Establishes a connection to a specified Internet server.
-
HttpOpenRequest
Creates an HTTP request handle.
-
HttpSendRequest
Sends the HTTP request to the server.
-
InternetReadFile
Reads data from the specified Internet resource.
-
InternetCloseHandle
Closes an opened Internet handle.
Example Usage
Here's a basic C++ example demonstrating how to fetch content from a URL using WinINet:
#include <windows.h>
#include <wininet.h>
#include <iostream>
int main() {
HINTERNET hInternet = InternetOpen(L"MyWinINetClient", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hInternet) {
std::cerr << "Failed to initialize WinINet." << std::endl;
return 1;
}
HINTERNET hUrl = InternetOpenUrl(hInternet, L"http://www.example.com", NULL, 0, INTERNET_FLAG_HYPERL, NULL);
if (!hUrl) {
std::cerr << "Failed to open URL." << std::endl;
InternetCloseHandle(hInternet);
return 1;
}
char buffer[1024];
DWORD bytesRead;
while (InternetReadFile(hUrl, buffer, sizeof(buffer) - 1, &bytesRead) && bytesRead > 0) {
buffer[bytesRead] = '\0';
std::cout << buffer;
}
InternetCloseHandle(hUrl);
InternetCloseHandle(hInternet);
std::cout << std::endl;
return 0;
}
Related Topics