我想你将不得不编写一个函数通过CURLOPT_WRITEFUNCTION
通过作为写回调(见this)。或者,您可以创建一个临时文件并通过CURLOPT_WRITEDATA
(该页面上列出的下一个选项)传递其文件描述符。然后,您会将临时文件中的数据读回到一个字符串中。不是最漂亮的解决方案,但至少你不必混淆缓冲区和函数指针。
编辑:既然你不想写一个文件,这样的事情可能工作:
#include <string>
size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) {
((string*)stream)->append((char*)ptr, 0, size*count);
return size*count;
}
int main(void) {
// ...
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.browsarity.com/");
string response;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
// The "response" variable should now contain the contents of the HTTP response
}
return 0;
}
免责声明:我没有测试过这一点,我有点生锈的C++,但你可以尝试一下。
我已经在那个链接,但我不明白如何做到这一点,指针和所有。 和文件的事情对我来说不好,因为我有少量的数据,我需要它非常快。 – shaimagz 2010-03-04 05:39:13
我不怪你。看我的编辑。 – 2010-03-05 22:59:23
适合我。我使用string *作为write_to_string中的最后一个参数类型,这为我节省了一个时间。 – Synox 2013-06-04 14:28:50