您的位置:首页 > 理论基础 > 计算机网络

C++ 下载文件 HTTP

2015-05-13 09:26 267 查看
参考: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: