C ++ CURL:以不同方式处理标题和正文数据

时间:2015-02-16 09:05:48

标签: c++ curl overloading

我希望编写一个C ++程序,将返回的标头保存到变量中,并将返回的主体保存到文本文件中。我怎么能这样做?

目前,我的方法是重载handleData函数,但编译器返回错误overloaded function with no contextual type information。这是我到目前为止所写的内容(代码摘录):

static size_t handleData(char *ptr, size_t size, size_t nmemb, string *str){ 
    string temp = string(ptr);
    // catch the cookie 
    if (temp.substr(0,10)=="Set-Cookie"){
       *str = temp;
    }
    return size * nmemb;
}

static size_t handleData(char *ptr, size_t size, size_t nmemb, FILE *stream){ 
    int written = fwrite(ptr, size, nmemb, stream);
    return written;
}

FILE *bodyfile;
string *return_header = new string;

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleData); 
curl_easy_setopt(curl, CURLOPT_HEADERDATA, return_header);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, bodyfile);

1 个答案:

答案 0 :(得分:2)

您应该使用CURLOPT_HEADERFUNCTION代替。

static size_t handleHeader(char *ptr, size_t size, size_t nmemb, string *str){ 
    // ...
}
static size_t handleData(char *ptr, size_t size, size_t nmemb, FILE *stream){ 
    // ...
}

curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, handleHeader);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleData); 
curl_easy_setopt(curl, CURLOPT_HEADERDATA, return_header);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, bodyfile);