Downloading files via http/ftp [C++]

Started by
6 comments, last by Atrix256 14 years, 7 months ago
Alright I've looked all over an can't find anything that is understandable could someone help me with this? I am trying to make a program that can download from a website or ftp even both. Any suggestions on how to do this? Not Looking for anyone to code it for me but maybe some hints or some links that might direct me to this type of thing. Thanks!
Advertisement
For what platform?
I found some articles on example-code.com with a quick search. Looks like you have to use their libraries, but it's a start. Links: FTP and HTTP.
You need to create a TCP socket, and connect to the web-server (usually on port 80). Then you send an HTTP-header, and the server will send the response to you.
The HTTP specification is available here: http://www.w3.org/Protocols/rfc2616/rfc2616.html.

The basic header you send is as below. \r\n is a carriage return, byte 0xD followed by 0xA, and means end of line. Two carriage returns means end of the header, and after you send that you will get a response from the server. The response will start with an HTTP response-header, followed by the file-data.

Request:
GET /filepath/file.html HTTP/1.1\r\nHost: www.the-server-address.com\r\n\r\n\r\n


Response:
HTTP/1.1 200 OK\r\nDate: ...\r\nContent-Type: text/html or something\r\nOther headers: ...\r\n\r\n\r\n<file data>


Try it out by connecting with telnet or something to a web-server and request a small file, which should be returned as text.
For Windows? Maybe try here.

Specifically, note the InternetReadFile function. I've never used that library (WinINet), but it appears to be perfectly acceptable.
libcurl would likely make your life a lot easier. Although, I needed asynchronous http file transfers (which are a bit of a pain in libcurl), and it wasn't that hard to roll my own on top of boost::asio.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I would like to second libcurl, especially if you are just looking for something that will "just do it". The hardest part of using libcurl was getting a version of it to compile as a static lib that would work with my app, though there are plenty of pre-compiled versions linked to by their website.

Once I got it compiling, it was literally a 20 minute process to learn everything I needed to hit a webpage and download the contents.
I want to "third" libcurl.

It's a great tool.

I shipped a game using it, and in fact if you look at spore, I believe it has the libcurl libraries shipped in there too (:

This topic is closed to new replies.

Advertisement