I'm looking for a simple example of how to download a file (HTML/image/whatever) from a site and provide a referrer.
Printable View
I'm looking for a simple example of how to download a file (HTML/image/whatever) from a site and provide a referrer.
I wrote this grabimage.c program last year that will download an image into a directory called /root/test if you give it the url of the image on the command-line. It also displays the header of the packet containing the image. (You may also need to look at the header file, mine.h, which is a bunch of generic functions only a few of which are used in grabimage.c)
Unfortunately, I didn't add much in the way of comments, but it may give you some idea of the complexities involved. It works this way:
- create a PF_INET socket and connect() to the remote server
- send a GET request for the image
- while receiving the response, watch for the "Content-Length:" line; this gives you the exact size of the image, and everything after that is the image itself.
- write the image to a local file
One complication I can recall is that your send/receive (or write/read) calls are not guaranteed to write or read as much as you want. This can be a screw up, particularly with the send, so you need to verify and complete the process:
This way, if send() only sent half of "line" on the first call, we loop around, advancing the "line" pointer appropriately, until the entire string has gone.Code:int Transmit (int sock, char *line) {
int done, todo=strlen(line);
while (todo>0) {
if ((done=send(sock,line,todo,0))==-1) return done;
todo-=done;
line+=done;
}
return 0;
}
Just use Win32 api.
1 line of code.
I think he lied... I can't even do it in 1 line of code and I made up a Winapi function :(Code:#include <windows.h>
int main(void) {
DownloadFileToThisDirectoryNow("http://site.com/file.exe", "C:\\file");
return 0;
}