Reputation: 73
Im trying to download a file from URL and save it in some path. I want to do it without curl, so I thinked via Sockets. I dont have any code, if you can help me thanks!
Upvotes: 1
Views: 1252
Reputation: 238431
C++ standard library does not have an API to interact with network sockets. As such, there is no standard way to download a file. To use the API provided by the operating system, the first step is to decide which operating system the program is going to run on.
The next step is to find out which application layer protocol to use. This can usually be determined by the scheme part of the URL. For example, if the url begins with http://
then your program needs to use the HTTP protocol. There is no standard HTTP protocol implementation in C++ either. You can find the specification here: https://www.rfc-editor.org/rfc/rfc2616 although you'll potentially also need to read some of the "obsoleted by" and "updated by" specifications as well if you need to support things such as HTTPS. That said (as is often the case), you can save a lot of time by using a free library instead, which I recommend.
Upvotes: 1
Reputation: 73
I found this code and works perfect.
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
int main(int argc, char *argv[]) {
// download the putty executable to putty.exe in the working directory, error checking omitted
URLDownloadToFile(NULL, "http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe", "putty.exe", 0, NULL);
return 0;
}```
Upvotes: 2