C++ 下載檔案 HTTP
阿新 • • 發佈:2019-01-08
參考:http://stackoverflow.com/questions/5184988/should-i-use-urldownloadtofile
#include "stdafx.h" #include <stdio.h> #include <tchar.h> #include <windows.h> #include <Urlmon.h> #pragma comment(lib,"Urlmon.lib") //加入連結庫 int _tmain(int argc, _TCHAR* argv[]) { TCHAR url[] = TEXT("http://a.hiphotos.baidu.com/image/pic/item/1b4c510fd9f9d72a9090781dd62a2834349bbb3e.jpg"); printf("Url: %S\n", url); TCHAR path[MAX_PATH]; GetCurrentDirectory(MAX_PATH, path); wsprintf(path, TEXT("%s\\index.jpg"), path); printf("Path: %S\n", path); HRESULT res = URLDownloadToFile(NULL, url, path, 0, NULL); if (res == S_OK) { printf("Ok\n"); } else if (res == E_OUTOFMEMORY) { printf("Buffer length invalid, or insufficient memory\n"); } else if (res == INET_E_DOWNLOAD_FAILURE) { printf("URL is invalid\n"); } else { printf("Other error: %d\n", res); } return 0; }