//--------------------------------------------------------------------------- #include #pragma hdrstop //--------------------------------------------------------------------------- #pragma link "wininet.lib" #pragma link "RzPrgres" //--------------------------------------------------------------------------- #include "HttpUnit.h" #include "RzPrgres.hpp" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- bool __fastcall THttpControl::HTTPUp(Char *URL, Char *Path, void* ProgressBar) { HINTERNET hInternet, hURL; HANDLE hFile= NULL; DWORD Size; DWORD dwRead, dwWritten, Recv=0; char buf[100000]; MSG Message; //ÀÎÅÍ³Ý ¿¬°á hInternet=InternetOpen(L"HTTPClient", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (hInternet==NULL) return false; //URLÀ» ¿¬´Ù. hURL=InternetOpenUrl(hInternet, URL, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (hURL==NULL) { InternetCloseHandle(hInternet); return false; } //´Ù¿î·Îµå ÁßÀÓ°ú Ãë¼Ò´Â ÇÏÁö ¾ÊÀ½À» Àü¿ªº¯¼ö¿¡ ±â·ÏÇÑ´Ù. bStopHttpDownLoad=false; bDownLoading=false; //·ÎÄà ÆÄÀÏÀ» »ý¼ºÇÑ´Ù. hFile=CreateFile(Path, GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); do { //´Ù¿î·Îµå¸¦ ¹Þ¾Æ ·ÎÄà ÆÄÀÏ¿¡ ±â·ÏÇÑ´Ù. InternetQueryDataAvailable(hURL, &Size, 0, 0); ReadFile(hFile, buf, Size, &dwRead, NULL); InternetWriteFile(hURL, buf, Size, &dwWritten); //´Ù¿î·Îµå ¹ÞÀº ¾çÀ» ¸Þ¼¼Áö·Î Àü´Þ Recv+=dwWritten; //Ãë¼Ò ó¸® if (bStopHttpDownLoad) { break; } } while(dwWritten!=0); InternetCloseHandle(hURL); InternetCloseHandle(hInternet); CloseHandle(hFile); bDownLoading=false; return true; } //--------------------------------------------------------------------------- bool __fastcall THttpControl::HTTPDown(Char *URL, Char *Path, void* ProgressBar, int Process_Percent) { HINTERNET hInternet = NULL, hURL = NULL; HANDLE hFile = NULL; DWORD Size; DWORD dwRead, dwWritten, Recv=0; char buf[100000]; MSG Message; String receive_data; DWORD BuffSize = 1024, Reserved = 0; int FileSize; bool bSizeIsKnown; char FileSizeBuff[1024]; float progress = 0.0; TRzProgressBar *pProgressBar =(TRzProgressBar*)ProgressBar; int ProgressBar_Percent = pProgressBar->Percent; //ÀÎÅÍ³Ý ¿¬°á hInternet=InternetOpen(L"HTTPClient", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (hInternet==NULL) return false; //URLÀ» ¿¬´Ù. hURL=InternetOpenUrl(hInternet, URL, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (hURL==NULL) { InternetCloseHandle(hInternet); return false; } //´Ù¿î·Îµå ÇÒ File size¸¦ È®ÀÎÇÑ´Ù. bSizeIsKnown = HttpQueryInfo(hURL, HTTP_QUERY_CONTENT_LENGTH, FileSizeBuff, &BuffSize, &Reserved); if (!bSizeIsKnown) goto fail; receive_data = FileSizeBuff; FileSize = receive_data.ToInt(); //´Ù¿î·Îµå ÁßÀÓ°ú Ãë¼Ò´Â ÇÏÁö ¾ÊÀ½À» Àü¿ªº¯¼ö¿¡ ±â·ÏÇÑ´Ù. bStopHttpDownLoad=false; bDownLoading=false; //·ÎÄà ÆÄÀÏÀ» »ý¼ºÇÑ´Ù. hFile=CreateFile(Path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); do { //´Ù¿î·Îµå¸¦ ¹Þ¾Æ ·ÎÄà ÆÄÀÏ¿¡ ±â·ÏÇÑ´Ù. InternetQueryDataAvailable(hURL, &Size, 0, 0); InternetReadFile(hURL, buf, Size, &dwRead); receive_data = buf; if (receive_data.Pos("404 Not Found") != 0) goto fail; WriteFile(hFile, buf, dwRead, &dwWritten, NULL); //´Ù¿î·Îµå ¹ÞÀº ¾çÀ» ¸Þ¼¼Áö·Î Àü´Þ Recv+=dwWritten; progress = (float)Process_Percent * (float)Recv / FileSize; pProgressBar->Percent = ProgressBar_Percent + progress; //Ãë¼Ò ó¸® if (bStopHttpDownLoad) { break; } } while(dwRead!=0); InternetCloseHandle(hURL); InternetCloseHandle(hInternet); CloseHandle(hFile); bDownLoading=false; return true; fail: if (hURL) InternetCloseHandle(hURL); if (hInternet) InternetCloseHandle(hInternet); if (hFile) CloseHandle(hFile); DeleteFile(Path); bDownLoading=false; return false; } //---------------------------------------------------------------------------