简体   繁体   English

从URL下载一个文件到C++的磁盘

[英]Downloading a file from URL to disk in C++

I have a simple question.我有一个简单的问题。 Is it possible to write simple code to download a file from the inte.net (from URL to disk) without using C++ (for mac osx) libraries like curl?是否可以编写简单的代码从 inte.net 下载文件(从 URL 到磁盘)而不使用 C++(对于 mac osx)库,如 curl? I have seen some examples but all of these use the Curl library.我看过一些例子,但所有这些都使用 Curl 库。

i use this code on my xcode projet..but i have some compilation (linking) errors我在我的 xcode projet 上使用此代码..但我有一些编译(链接)错误

    #define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(void) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = "http://localhost/aaa.txt";
    char outfilename[FILENAME_MAX] = "bbb.txt";
    curl = curl_easy_init();
    if (curl) {
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    return 0;
}

how can i link the curl library to my xcode project?如何将 curl 库链接到我的 xcode 项目?

You can launch a console command, it is very simple:D您可以启动控制台命令,这非常简单:D

system("curl -o ...")

or要么

system("wget ...")

You have to learn bits of "socket programming" and implement a very basic HTTP protocol;您必须学习一些“套接字编程”并实现一个非常基本的 HTTP 协议; the minimalist thing is to send string like "GET /this/path/to/file.png HTTP/1.0\r\n" to the site;最简单的做法是向站点发送类似"GET /this/path/to/file.png HTTP/1.0\r\n"的字符串; then, likely it will answer with an HTTP header you have to parse to know at least the length of the binary data following (if the request succeeded, otherwise you have to handle HTTP errors, or a unexpected contet-type like a html page).然后,它可能会回答 HTTP header 你必须解析以至少知道后面的二进制数据的长度(如果请求成功,否则你必须处理 HTTP 错误,或者像 html 页面这样的意外内容类型) .

This guide should give you the basic to start with;本指南应该为您提供基础知识; about HTTP , it depends on your need, sometimes sending a "raw" GET could suffice, sometimes not.关于HTTP ,这取决于您的需要,有时发送“原始” GET 就足够了,有时则不够。

EDIT编辑

Changed to pretend that the request comes from a HTTP/1 compliant client, since HTTP/1.1 wants the Host header to be sent, as commenter has rightly pointed.更改为假装请求来自 HTTP/1 兼容客户端,因为 HTTP/1.1 希望发送主机 header,正如评论者正确指出的那样。

EDIT2编辑2

The OP changed the question, which became something about how to link with a library in Xcode. There's already a similar question on SO . OP 改变了问题,它变成了关于如何与 Xcode 中的库链接的问题。SO 上已经有一个类似的问题。

"Downloading a file from URL" means basically doing an GET request to some remote HTTP server. “从 URL 下载文件”基本上意味着向某个远程 HTTP 服务器发出GET请求。 So you need to have your application know how to do that HTTP request.因此,您需要让您的应用程序知道如何执行 HTTP 请求。

But HTTP is now a quite complex protocol.但是HTTP现在是一个相当复杂的协议。 Its specification alone is long and complex (more than a hundred pages).仅其规范一项就又长又复杂(超过一百页)。 libcurl is a good library implementing it. libcurl是一个很好的实现它的库。

Why do you want to avoid using a good free library implementing a complex protocol?为什么要避免使用实现复杂协议的优秀免费库? Of course, you could implement the complex HTTP protocol by yourself (probably that needs years of work), or make a minimal program which don't implement all the details of HTTP protocol but might work (but won't work with weird HTTP servers).当然,您可以自己实现复杂的 HTTP 协议(可能需要多年的工作),或者制作一个最小程序,它不实现 HTTP 协议的所有细节但可能有效(但不适用于奇怪的 HTTP 服务器).

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

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