Thread: Downloading a file via HTTP?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    Downloading a file via HTTP?

    I'm looking for a simple example of how to download a file (HTML/image/whatever) from a site and provide a referrer.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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:
    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;
    }
    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.
    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

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Just use Win32 api.
    1 line of code.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Quote Originally Posted by Alex31 View Post
    Just use Win32 api.
    1 line of code.
    1 line? How's that work?

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Code:
    #include <windows.h>
    
    int main(void) {
       DownloadFileToThisDirectoryNow("http://site.com/file.exe", "C:\\file");
       return 0;
    }
    I think he lied... I can't even do it in 1 line of code and I made up a Winapi function
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Downloading a file though HTTP requests
    By ColdFire in forum Windows Programming
    Replies: 3
    Last Post: 05-25-2003, 01:05 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM