2

I have few PC's that run on slow Internet connection network + they use VPNs so the connection becomes really slow. I have my own app that must download updates from external server via HTTP, but if the update is around few MBs it doesn't get downloaded. So what i am asking is how to implement download method so i can resume the download if the connection is aborted.

I code on Windows, C++, it would be good if i can achieve this using WinAPI.

I think internet explorer has this feature, so it must be implemented with WININET.

3
  • 2
    What have you tried so far? There's hundreds of examples of how to download stuff on the net.
    – Mat
    Commented May 6, 2012 at 11:53
  • 2
    Why are you so keen on using WINAPI when there are decent platform-independent API's that can do this for you (and often are even more easy to use). There's for example the very popular libcurl to begin with.
    – KillianDS
    Commented May 6, 2012 at 12:00
  • 2

1 Answer 1

14

HTTP resume is somewhat a misnomer as the connection is usually terminated when the download is "paused" and then when it is resumed, a new request is made.

The new request then contains the range specification, so the server only sends a portion of the whole requested file.

Usually you would first fire a HEAD request to get the whole filesize.

Request

HEAD /big_file.zip HTTP/1.1
Host: www.example.com

Response

HTTP/1.0 200 OK
Accept-Ranges: bytes
Content-Length: 2000000
Content-Type: application/zip

Then you can send a request like this to only download bytes from 1,000,000 to 2,000,000 (if your first download stopped at 1,000,000 bytes):

Request

GET /big_file.zip HTTP/1.1
Host: example.com
Range: bytes=1000000,2000000

Response

HTTP/1.0 206 Partial Content
Accept-Ranges: bytes
Content-Length: 1000000
Content-Range: bytes 1000000-2000000/2000000
Content-Type: image/jpeg

...

That would be the general principle. You can implement it in C++ using cUrl, boost::asio, Windows sockets, ... There are many different ways and many good tutorials.

1
  • WinInet also supports this by added the header required header strings.
    – Deanna
    Commented May 8, 2012 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.