curl-library
Re: Getting a file on http using CURL into IStream
Date: Fri, 2 Aug 2013 09:06:19 -0500
On Fri, Aug 2, 2013 at 8:29 AM, Ali Nikzad wrote:
> I want to read a file with the HTTP protocol using the CURL library and
> write it to an IStream. I found this thread but it recommends using the
> boost library for the stream. Can I get it using just CURL and C++ standard
> library?
Looks like the code on stack overflow is not really the right approach
if you're just doing
regular HTTP. I don't really know what is a "IStream" but libcurl's
write callback will let
you pass in a pointer to whatever data structure or "object" you want to use.
The example at http://curl.haxx.se/libcurl/c/getinmemory.html shows
how to do it with a
user-defined C struct, but you should be able to easily adapt it to
your IStream, something
like this:
#include <curl/curl.h>
static size_t WriteMemoryCallback(void *contents, size_t size, size_t
nmemb, void *userp)
{
size_t realsize = size * nmemb;
IStream *stream=(IStream *)userp;
// Append the "contents" pointer to your IStream here,
// its length will be size*nmemb
return realsize;
}
int main(void)
{
CURL *curl_handle;
CURLcode res;
IStream stream;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/");
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)&stream);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl_handle);
if(res == CURLE_OK) {
// You stream should contain the file now.
} else {
// Handle the error
}
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
return 0;
}
- Jeff
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-08-02