1

I have a simple question. Is it possible to write simple code to download a file from the internet (from URL to disk) without using C++ (for mac osx) libraries like curl? I have seen some examples but all of these use the Curl library.

i use this code on my xcode projet..but i have some compilation (linking) errors

    #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?

Safari
  • 11,437
  • 24
  • 91
  • 191
  • 2
    You might provide the reason for not-using curl, so others can show you a workaround for your specific problem. – phimuemue Apr 07 '12 at 08:29
  • It's possible, but it's not simple. For example, you'll either have to support chunked encoding (which is not simple) or not issue HTTP 1.1 requests (which makes you interoperate badly). That's a bad tradeoff to have to make. What's your outer problem? – David Schwartz Apr 07 '12 at 08:31
  • 1
    I need a simple script to test .. I would not install on my mac this library for this simple test.. – Safari Apr 07 '12 at 08:38
  • 3
    @GgSalent all Macs come with cURL (and libcurl) pre installed. You don't need to install anything. Don't reinvent the wheel; use a library like libcurl. Really, libcurl will be faster and more reliable than the code you'll write yourself if you don't use it. –  Apr 07 '12 at 08:40
  • I understand when people can't get a complex badly written question. I don't understand when a simple, clear question gets dressed too much so to "avoid" plain, simple, direct answers. Is it possible to write simple code to download a file form internet? **Yes**. The Yes answer, and given simple examples, do not imply *it works always like using cURL*. To handle all the cases, you have to implement basically what others have already well implemented (e.g. libcurl); there's no point in doing it, that's obvious, but being courious about possibilities is always good. – ShinTakezou Apr 07 '12 at 08:48
  • I modified my question with a sample found on the net .. – Safari Apr 07 '12 at 09:08
  • and you have changed the question, making some answers not answering your question anymore! I don't think it is prohibited or what, though I thnk it should be done infrequently... – ShinTakezou Apr 07 '12 at 09:27
  • have you considered libwww or libfetch? I think libfetch must be built into the Mac OS X.. – Ashwin Apr 07 '12 at 09:40
  • i have solved: i added thi flag -L/usr/lib -lcurl -lssl -lcrypto -lz in my xcode file project: Project -> Edit Project -> Linking -> Other Linker Flags – Safari Apr 07 '12 at 09:47
  • How does linking with curl solve your problem of downloading a file without using libraries like curl? – David Schwartz Apr 07 '12 at 11:36

3 Answers3

2

You can launch a console command, it is very simple :D

system("curl -o ...")

or

system("wget ...")
dash1e
  • 7,677
  • 1
  • 30
  • 35
1

You have to learn bits of "socket programming" and implement a very basic HTTP protocol; the minimalist thing is to send string like "GET /this/path/to/file.png HTTP/1.0\r\n" to the site; 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).

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.

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.

EDIT2

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.

Community
  • 1
  • 1
ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
  • sometimes sending a "raw" GET *could* suffice (I've written). I forget the Host, reverting to HTTP/1.0. Me, I don't want for sure, in fact I use cURL if I need. I don't know what the user want, but by the question it seems it want to *try* something like my answer shows. – ShinTakezou Apr 07 '12 at 08:35
1

"Downloading a file from URL" means basically doing an GET request to some remote HTTP server. So you need to have your application know how to do that HTTP request.

But HTTP is now a quite complex protocol. Its specification alone is long and complex (more than a hundred pages). libcurl is a good library implementing it.

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).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547