开发者

CURL C API: callback was not called

The code below is a test for the CURL C API . The problem is that the callback function write_callback is never called. Why ?

/** compilation: g++ source.cpp -lcurl */

#include <assert.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <curl/curl.h>

using namespace std;

static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
    std::cerr << "CALLBACK WAS CALLED" << endl;
    exit(-1);
    return size*nmemb;
}

static void test_curl()
{
    int any_data=1;
    CURLM* multi_handle=NULL;
    CURL* handle_curl = ::curl_easy_init();
    assert(handle_curl!=NULL);
    ::curl_easy_setopt(handle_curl, CURLOPT_URL, "http://en.wikipedia.org/wiki/Main_Page");
    ::curl_easy_setopt(handle_curl, CURLOPT_WRITEDATA, &any_data);
    ::curl_easy_setopt(handle_curl, CURLOPT_VERBOSE, 1);
    ::curl_easy_setopt(handle_curl, CURLOPT_WRITEFUNCTION, write_callback);
    ::curl_easy_setopt(handle_curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

    multi_handle = ::curl_multi_init();
    assert(multi_handle!=NULL);
    ::curl_multi_add_handle(multi_handle, handle_curl);
    int still_running=0;
    /* lets start the fetch */
    while(::curl_multi_perform(multi_handle, &still_running) ==
          CURLM_CALL_MULTI_PERFORM );
    std::cerr << "End of curl_multi_perform."<< endl;
    //cleanup should go here
    ::exit(EXIT_SUCCESS);
}

int main(int argc,char** argv)
{
    test_curl();
    return 0;
}

Man开发者_如何学Cy thanks

Pierre


You need to check the value of still_running and call curl_multi_perform() again if there are still pending operations.

Simple example:

int still_running=0;
/* lets start the fetch */
do {
    while(::curl_multi_perform(multi_handle, &still_running) ==
        CURLM_CALL_MULTI_PERFORM);
} while (still_running);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜