How to programmatically download files from the Internet?

How are files downloaded from servers into programming languages ​​such as C? I understand that higher-level languages ​​have magical functions such as "download_file_from_url ()", but they do not help me understand what is really happening. I am a little familiar with sockets, but network programming in general is still a black box for me. Thanks for any help.

+3
source share
6 answers

Basically, at a low level, the program opens a socket on port 80 (usually) on the server and sends it a request that looks something like this:

GET /index.html HTTP/1.1
Host: stackoverflow.com

... followed by an empty string.

, , . HTTP 1.1 ( , ); HTTP 1.0 Connection: close, .

HTTP, , (all-in-one-page ). , , telnet ( , , ). telnet stackoverflow.com 80, . Enter .

. , . (, www.stackoverflow.com stackoverflow.com β€” " ", SO , SO stackoverflow.com, www.stackoverflow.com. " " .. ..).

+12

libcurl - , , .

+9

" ", , : -)

C, , - , . ( , ).

C libcurl

+4

HTTP, RFC on HTTP ( ..), FTP - RFC FTP ( , PWD, CD ..). , .

+1

(, - ..), :

  • DNS-, URL IP

  • IP- URL (80 http)

  • HTTP-

  • HTTP-

  • Process the response correctly, and if the response contains data for the file, cancel the response and save the data in a temporary file

  • When the file is fully downloaded, close the connection and move the full temp file to the desired location.

+1
source

Use a library like libcurl .

0
source

Source: https://habr.com/ru/post/1744359/


All Articles