C/C+http 다운로드 파일 구현
#include
#include
#define RECVPACK_SIZE 2048
bool DownloadSaveFiles(char* url,char *strSaveFile) {//
bool ret=false;
CInternetSession Sess("lpload");
Sess.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT , 2000); //2
Sess.SetOption(INTERNET_OPTION_SEND_TIMEOUT , 2000); //2
Sess.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT , 2000); //2
Sess.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT , 2000); //2
Sess.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 2000); //2
DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD ;
CHttpFile* cFile = NULL;
char *pBuf = NULL;
int nBufLen = 0 ;
do {
try{
cFile = (CHttpFile*)Sess.OpenURL(url,1,dwFlag);
DWORD dwStatusCode;
cFile->QueryInfoStatusCode(dwStatusCode);
if (dwStatusCode == HTTP_STATUS_OK) {
//
DWORD nLen=0;
cFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, nLen);
//CString strFilename = GetFileName(url,TRUE);
nBufLen=nLen;
if (nLen <= 0) break;//
//
pBuf = (char*)malloc(nLen+8);
ZeroMemory(pBuf,nLen+8);
char *p=pBuf;
while (nLen>0) {
// 8K
int n = cFile->Read(p,(nLenClose();
Sess.Close();
delete cFile;
}
return ret;
}
int main() {
DownloadSaveFiles("http://www.nirsoft.net/utils/nircmd.zip","d:\\cppdld_nircmd.zip");
return 0;
}
2.WinInet API 사용
void download(const char *Url, const char *filename)
{
byte Temp[1024];
ULONG Number = 1;
FILE *stream;
HINTERNET hSession = InternetOpen("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET handle2 = InternetOpenUrl(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (handle2 != NULL)
{
if ((stream = fopen(filename, "wb")) != NULL)
{
while (Number > 0)
{
InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);
fwrite(Temp, sizeof(char), Number, stream);
}
fclose(stream);
}
InternetCloseHandle(handle2);
handle2 = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
}
3.윈도 가 제공 하 는 urlmon 라 이브 러 리 사용
#include
#include
#include
#include
#include
#include
#pragma comment(lib, "urlmon.lib")
using namespace std;
BOOL FileExistsStatus(const CHAR* path)
{
DWORD dwAttribute = GetFileAttributes(path);
if (dwAttribute == 0XFFFFFFFF) return false; //0XFFFFFFFF
else return true;
}
BOOL DownloadFiles(const CHAR* url, const CHAR* downloadPath)
{
if (URLDownloadToFile(NULL, url, downloadPath, 0, 0) == S_OK && FileExistsStatus(downloadPath)) return true;
else return false;
}
int main(int argc, char* argv[])
{
if (DownloadFiles(argv[1], argv[2])) printf("OK!
");
else printf("Error!
");
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
08.Const / Void텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.