1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
using namespace std;
#include <iostream>
#include <fstream>
#include <wininet.h>
char DownloadURL[]="http://AddresseDuFichier";
int main(void)
{
HINTERNET InternetSession=InternetOpen("Unamed",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
if (!InternetSession)
{
cout << "Cannot open Internet\n";
return 0;
}
HINTERNET OpenUrl=InternetOpenUrl(InternetSession,DownloadURL,NULL,0,0,0);
if (!OpenUrl)
{
cout << "Cannot open URL " << DownloadURL << "\n";
return 0;
}
BOOL Test1,Test2;
DWORD RemainingBytes;
ofstream File;
File.open("Downloaded.dat",ios::binary);
do
{
Test1=InternetQueryDataAvailable(OpenUrl,&RemainingBytes,0,0);
cout << "Remaining bytes: " << RemainingBytes << "\n";
if (!Test1)
{
cout << "Cannot query data. Error " << GetLastError() << "\n";
return 0;
}
char *Buffer=new char[RemainingBytes];
char *Ptr=Buffer;
DWORD BytesToRead=RemainingBytes;
DWORD BytesRead;
do
{
Test2=InternetReadFile(OpenUrl,Ptr,BytesToRead,&BytesRead);
cout << "Bytes read: " << BytesRead << "\n";
if(!Test2)
{
cout << "Cannot read data. Error " << GetLastError() << "\n";
return 0;
}
BytesToRead-=BytesRead;
Ptr+=BytesRead;
} while(Test2 && BytesRead);
File.write(Buffer,RemainingBytes);
delete []Buffer;
} while(Test1 && RemainingBytes);
File.close();
InternetCloseHandle(OpenUrl);
InternetCloseHandle(InternetSession);
return 0;
} |
Partager