#include #include #include #pragma comment(lib, "wininet.lib") int main() { HINTERNET hInternetSession = InternetOpen(L"MyApp", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (hInternetSession != NULL) { HINTERNET hUrlHandle = InternetOpenUrl(hInternetSession, L"https://pixta.jp/img/og-image.jpg", NULL, 0, INTERNET_FLAG_RELOAD, 0); if (hUrlHandle != NULL) { DWORD dwStatusCode = 0; DWORD dwStatusCodeSize = sizeof(dwStatusCode); if (HttpQueryInfo(hUrlHandle, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatusCode, &dwStatusCodeSize, NULL)) { if (dwStatusCode == HTTP_STATUS_OK) { char szBuffer[1024]; DWORD dwBytesRead = 0; HANDLE hFileHandle = CreateFile(L"image.jpg", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFileHandle != INVALID_HANDLE_VALUE) { while (InternetReadFile(hUrlHandle, szBuffer, sizeof(szBuffer), &dwBytesRead) && dwBytesRead) { DWORD dwBytesWritten = 0; if (!WriteFile(hFileHandle, szBuffer, dwBytesRead, &dwBytesWritten, NULL)) { printf("Failed to write to file\n"); break; } } CloseHandle(hFileHandle); } else { printf("Failed to create file\n"); } } else { printf("HTTP error: %d\n", dwStatusCode); } } else { printf("Failed to query HTTP status code\n"); } InternetCloseHandle(hUrlHandle); } else { printf("Failed to open URL\n"); } InternetCloseHandle(hInternetSession); } else { printf("Failed to initialize WinINet\n"); } return 0; }