gpt4 book ai didi

c++ - libcURL - 如何在 C++ 中停止输出到命令行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:15 25 4
gpt4 key购买 nike

如何防止/删除/停止打印/输出到 libcurl 中的命令行?出于某种原因,以下代码适用于 URL,但是当我访问带有图像的页面时,例如 http://192.168.123.123/banana.gif ,然后它崩溃了:

CURL *session;
session = curl_easy_init();
curl_easy_setopt(session, CURLOPT_URL, "http://192.168.123.123/banana.gif");

CURLcode curl_code = curl_easy_perform (session);
long http_code = 0;
curl_easy_getinfo(session, CURLINFO_RESPONSE_CODE, &http_code);

最佳答案

这是因为你没有设置CURLOPT_WRITEDATA选项:

The internal CURLOPT_WRITEFUNCTION will write the data to the FILE * given with this option, or to stdout if this option hasn't been set.

如果您决定完全忽略响应数据,您可以将其写入/dev/null:

/* ... */

FILE *devnull = fopen("/dev/null", "w+");
curl_easy_setopt(session, CURLOPT_WRITEDATA, devnull);
CURLcode curl_code = curl_easy_perform(session);
fclose(devnull);

/* ... */

另一种选择是使用 NOOP write function :

curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, noop_cb);

write 函数只返回接收到的字节数:

size_t noop_cb(void *ptr, size_t size, size_t nmemb, void *data) {
return size * nmemb;
}

关于c++ - libcURL - 如何在 C++ 中停止输出到命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com