I'm looking for a simple example of how to download a file (HTML/image/whatever) from a site and provide a referrer.
This is a discussion on Downloading a file via HTTP? within the Networking/Device Communication forums, part of the General Programming Boards category; I'm looking for a simple example of how to download a file (HTML/image/whatever) from a site and provide a referrer....
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; }
Last edited by MK27; 03-13-2009 at 02:48 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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 functionCode:#include <windows.h> int main(void) { DownloadFileToThisDirectoryNow("http://site.com/file.exe", "C:\\file"); return 0; }![]()
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.