InternetOpen と InternetReadFile を使用することでHTTPファイルを読み込むことができます。
入力されたアドレスのファイルを取得し、最初の1024バイトを入力されたファイル名に保存する。
#include <windows.h>
#include <tchar.h>
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
int _tmain()
{
LPCTSTR lpszUrl = TEXT("http://sample.net/");
LPCTSTR lpszPath = TEXT("d:\\sample.html");
HINTERNET hInternet = InternetOpen(TEXT("Agent"), 0, NULL, NULL, 0);
HINTERNET hFile = InternetOpenUrl(hInternet, lpszUrl, NULL, 0, 0, 0);
if(hFile != NULL) {
HANDLE hWriteFile = CreateFile(lpszPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hWriteFile != INVALID_HANDLE_VALUE) {
BYTE data[16];
DWORD dwTotal = 0;
DWORD dwBytesToRead;
DWORD dwWritten;
do {
if(!InternetReadFile(hFile, &data, 16, &dwBytesToRead)) {
break;
}
dwTotal += dwBytesToRead;
WriteFile(hWriteFile, &data, dwBytesToRead, &dwWritten, NULL);
} while(dwBytesToRead == 16 && dwTotal < 1024);
CloseHandle(hWriteFile);
}
InternetCloseHandle(hFile);
}
InternetCloseHandle(hInternet);
return 0;
}