Am I doing something wrong? I've tried almost everything!
ret = (BYTE*) malloc (contentlen + 1);
while(InternetReadFile(hIurl, buf, sizeof(buf), &numrcved))
{
ret =+ buf;
total =+ numrcved;
//printf((char*)ret); //Works Here
if(numrcved==0) break;
}
}
InternetCloseHandle(hIurl);
InternetCloseHandle(hInet);
printf((char*)ret); //Not Here
free(ret);
Full Code:
// A file download subsystem
#include <Windows.h>
#include <iostream>
#include <WinInet.h>
#pragma comment (lib, "Wininet")
using namespace std;
bool httpverOK(HINTERNET hIurl)
{
char str[80];
unsigned long len = 79;
if(!HttpQueryInfo(hIurl, HTTP_QUERY_VERSION, &str, &len, NULL))
return false;
// First, check major version number
char *p = strchr(str, '/');
p++;
if(*p == '0')
return false; // can't use HTTP 0.x
// Now, find start of minor HTTP version number
p = strchr(str, '.');
p++;
// convert to int
int minorVerNum = atoi(p);
if(minorVerNum > 0)
return true;
return false;
}
bool getfname(char *url, char *fname)
{
// Find last slash /
char *p = strrchr(url, '/');
// Copy filename afther the last slash
if(p && (strlen(p) < 512))
{
p++;
strcpy_s(fname, 256, p);
return true;
}
else
{
return false;
}
}
// Confirm that the URL specifies HTTP
bool ishttp(char *url)
{
char str[5] = "";
// get the first four characters from the URL
strncpy_s(str, url, 4);
// convert to lowercase
for(char *p = str; *p; p++)
*p = tolower(*p);
return !strcmp("http", str);
}
unsigned long openfile(char *url, FILE * pFile)
{
char fname[512];
if(getfname(url, fname))
{
fopen_s(&pFile, fname, "w");
if(pFile != NULL)
{
fseek(pFile, 0L, SEEK_END);
long sz = ftell(pFile) ;
fseek(pFile, 0L, SEEK_SET);
return sz;
}
}
return 0;
}
bool download(char *url)
{
BYTE buf[10240];// input buffer
unsigned long numrcved; // number of bytes read
unsigned long filelen; // length of the file on disk
HINTERNET hIurl;
HINTERNET hInet; // internet handles
unsigned long contentlen; // length of content
unsigned long len; // length of contentlen
unsigned long total = 0; // running total of bytes received
char header[80]; // holds Range header
if(ishttp(url))
{
FILE * pFile;
char fname[512];
getfname(url, fname);
fopen_s(&pFile, fname, "wb");
filelen = openfile(url, pFile) ;
if(InternetAttemptConnect(0) == ERROR_SUCCESS)
{
hInet = InternetOpen("downloader", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(hInet != NULL)
{
sprintf_s(header, "Range:bytes=%d-", filelen);
hIurl = InternetOpenUrl(hInet, url, header, strlen(header), INTERNET_FLAG_NO_CACHE_WRITE, 0);
if(hIurl != NULL)
{
int total = 0;
BYTE *ret = NULL;
len = sizeof contentlen;
if(HttpQueryInfo(hIurl, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &contentlen, &len, NULL))
{
if(filelen!= contentlen )
{
DWORD dataSize = 0;
ret = (BYTE*) malloc (contentlen + 1);
while(InternetReadFile(hIurl, buf, sizeof(buf), &numrcved))
{
ret =+ buf;
total =+ numrcved;
//printf((char*)ret); //Works Here
if(numrcved==0) break;
}
}
InternetCloseHandle(hIurl);
InternetCloseHandle(hInet);
printf((char*)ret); //Not Here
free(ret);
return true;
}
}
}
}
}
return false;
}
int main(int argc, char *argv[])
{
char url[] = "http://download.tuxfamily.org/notepadplus/5.9.8/npp.5.9.8.Installer.exe";
printf("Beginning download\n");
if(download(url))
{
printf("Download Complete\n");
}
system("PAUSE");
return EXIT_SUCCESS;
}
This post has been edited by juryben: 03 March 2012 - 02:10 PM

New Topic/Question
Reply




MultiQuote






|