简体   繁体   English

C / C ++-如何从网上按字节下载字节(如文件流)?

[英]C / C++ - How to download byte by byte (like a file stream) from the web?

I'm building a simple download and file transfer manager in C and C++, with support for pause and resume operations. 我正在用C和C ++构建一个简单的下载和文件传输管理器,并支持暂停和恢复操作。 I looked for a way to download byte by byte from a given URL, but all I could find were references to external libraries like libcurl. 我寻找一种从给定URL逐字节下载的方法,但是我所能找到的只是对外部库(如libcurl)的引用。 I don't want external libraries, I want to manually read and write the bytes in a similar way used to read and write bytes to files (like ifstream in C++, and fopen in C) using only the C/C++ native library. 我不需要外部库,我想以仅使用C / C ++本机库的方式来读写字节,就像读取和写入文件的字节一样(例如C ++中的ifstream和C语言中的fopen)。 The pseudo-code for what I need would be: 我需要的伪代码是:

void download(file, url) {
    file.open();
    url.open();
    set_active(true);
    while( ! pause() ) {
        bytes_read = url.read(byte_array);
        if(bytes_read < 1)
            break;
        file.write(byte_array, bytes_read);
    }
    url.close();
    file.close();
    set_active(false);
}

How would that pseudo-code work in both C and C++? 伪代码在C和C ++中将如何工作?


EDIT: I don't want external libraries for the simple interest on learning the capabilities and limits of both languages and their native libraries. 编辑:我不希望外部库对学习两种语言及其本机库的功能和限制有简单的兴趣。

EDIT 2: Conclusion: Neither of the languages have native support for HTTP operations, therefore I have to choose between a using third-party library or writing a HTTP (and several other protocols) implementation from the scratch, which is unreliable. 编辑2:结论:两种语言都没有对HTTP操作的本机支持,因此我必须在使用第三方库还是从头开始编写HTTP(以及其他几种协议)实现之间进行选择,这是不可靠的。 Thanks. 谢谢。

You should use libcurl or some other HTTP client library (eg libsoup if using GTK). 您应该使用libcurl或其他HTTP客户端库(例如,如果使用GTK, 则为libsoup )。 If you need HTTP server support, you can find libraries for that, eg libonion . 如果需要HTTP服务器支持,则可以找到相应的库,例如libonion

You might also consider POCO or Qt5 frameworks, they both have HTTP things. 您可能还会考虑POCOQt5框架,它们都具有HTTP功能。

If you don't want to use any such HTTP 3 rd party libraries, you'll need to re-implement HTTP by yourself. 如果你不希望使用任何HTTP等第三方库,你需要自己重新实现HTTP。 That might be complex (and several months or years of work), since the HTTP specifications are several hundreds pages! 因为HTTP规范有数百页,所以这可能很复杂(需要花费数月或数年的时间)! Handling some simplistic GET requests is easy, but handing all the HTTP specification is hard. 处理一些简单的GET请求很容易,但是处理所有HTTP规范却很困难。

You could try to implement your subset of HTTP which might work in some cases, but you'll easily find HTTP servers or clients needing more of HTTP, .... And your implementation would be much more brittle and buggy than existing HTTP libraries... 您可以尝试实现HTTP 子集 ,该子集某些情况下可能会起作用,但是您会轻松地发现需要更多HTTP的HTTP服务器或客户端,....而且与现有HTTP库相比,您的实现将更加脆弱且容易出错。 ..

Just noticed your edited requirement that you don't want external libraries. 刚刚注意到您编辑过的要求,即您不需要外部库。 But here are extracts from a program I wrote in MSVC to download files from a Windows program. 但是这里是我用MSVC编写的从Windows程序下载文件的程序的摘录。

// Windows Header Files:
#include <windows.h>
#include <wininet.h>

// Make internet connection.
HINTERNET hInternetSession = InternetOpen(
        "Microsoft Internet Explorer",          // agent
        INTERNET_OPEN_TYPE_PRECONFIG,           // access
        NULL, NULL, 0);                         // defaults
...

// Make connection to desired page.
HINTERNET hURL = InternetOpenUrl(
        hInternetSession,                       // session handle
        szSors,                                 // URL to access
        NULL, 0, 0, 0);                         // defaults
if (hURL == 0)
    return 0;
...

// Read data into memory buffer.
DWORD dwBytesRead = 0;
BOOL bResult = InternetReadFile(
        hURL,                                   // handle to URL
        (LPSTR)cBuffer,                         // pointer to buffer
        (DWORD)MYBSIZE,                         // size of buffer
        &dwBytesRead);                          // to hold return value
...

InternetCloseHandle(hInternetSession);

I never downloaded one byte as you ask, but I don't see why you could not. 我从未按照您的要求下载过一个字节,但我看不到您为什么不能下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM