Ok, here's the update. I segregated the actual process of retrieving the file, and saving it into it's own function, so you can just call that instead. More lucid error handling is probably needed, as right now it just indicates success or failure. Also, as I wrote this in C, there's really nothing C++ about it, it doesn't take advantage of any of C++'s advanced features. It will, however, compile without issue in C++ if you cast the return value of the malloc() call. I don't cast malloc calls in C, because the C standard defines that a void * can be assigned any type, and I only need to cast it to the appropriate type when dereferencing the value. All you need to do is call InternetSaveFile with the url, and the local filename as the parms, and simply access the local filename using conventional methods (open()/fopen()/ifstream, whatever). I've also posted a properly formatted version
here for the time being. Hope this helps.
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HTTP_USER_AGENT "HTTPRIP"
#define BUFFER_SIZE 512
void usage(void);
int InternetSaveFile(const char *url, const char *filename);
int main(int argc, char **argv)
{
FILE *input = NULL;
char buffer[BUFFER_SIZE] = { 0 };
if (argc != 3)
{
usage();
exit(EXIT_FAILURE);
}
if (InternetSaveFile(argv[1], argv[2]))
{
input = fopen(argv[2], "r");
if (input != NULL)
{
while (fgets(buffer, BUFFER_SIZE, input) != NULL)
printf("%s", buffer);
fclose(input);
}
else
printf("Unable to open input file!\n");
}
else
printf("Failure!\n");
return 0;
}
void usage(void)
{
fprintf(stderr, "http rip - Chad Osgood\n\nusage: httprip url filename\n");
}
int InternetSaveFile(const char *requesturl, const char *outputfile)
{
const char http[] = "HTTP";
HANDLE file;
SECURITY_ATTRIBUTES sa;
HINTERNET internet;
HINTERNET url;
char *buf = NULL;
DWORD bytesread;
DWORD byteswritten;
int k;
// validate protocol -- first 4 alphas should be http, case insensitive
for (k = 0; k < 4; k++)
if (requesturl[k] != http[k] && requesturl[k] != (char)(http[k] + 32))
return 0; // need error handling
file = CreateFile(outputfile,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (file == INVALID_HANDLE_VALUE)
return 0; // need error handling
internet = InternetOpen(HTTP_USER_AGENT,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0);
if (internet == NULL)
return 0; // need error handling
url = InternetOpenUrl(internet,
requesturl,
NULL,
0,
INTERNET_FLAG_PRAGMA_NOCACHE,
0);
if (url == NULL)
return 0; // need error handling
buf = malloc(BUFFER_SIZE + 1);
if (buf == NULL)
return 0; // need error handling
while (InternetReadFile(url,
buf,
BUFFER_SIZE,
&bytesread))
{
if (bytesread == 0) break;
WriteFile(file,
buf,
bytesread,
&byteswritten,
NULL);
}
free(buf);
InternetCloseHandle(url);
InternetCloseHandle(internet);
CloseHandle(file);
return 1;
}
[edit]Also, I currently have it printing the contents of the buffer to stdout after the call to fgets(), but you'll probably want to kill that...[/edit]