互帮互助,小bug记录:libcurl timeout不起作用,程序无法结束

编程入门 行业动态 更新时间:2024-10-27 14:23:47

互帮互助,小bug记录:libcurl timeout<a href=https://www.elefans.com/category/jswz/34/1771377.html style=不起作用,程序无法结束"/>

互帮互助,小bug记录:libcurl timeout不起作用,程序无法结束

libcurl的timout超时功能可能会失效,一种是若禁用SIGNAL的话,DNS查询可能会超过timeout设置的极限时间,这种官网上有说明,一种是我今天碰到的,如果使用POST请求,但是没有传递POST请求体,那么也会阻塞住,而非传递一个空POST请求体,这个官网上没有说明,我也没有查到其他人提问,应该是个bug,记录在此,方便其他人.

以下是会阻塞的代码:

	// The timeout for this code will not workCURL* curl = curl_easy_init();struct curl_slist* list = nullptr;if (curl != nullptr) {char buf[CURL_ERROR_SIZE];curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, buf);curl_easy_setopt(curl, CURLOPT_URL, "");curl_easy_setopt(curl, CURLOPT_POST, 1L);list = curl_slist_append(list, "Content-Type: application/json");list = curl_slist_append(list, "Accept: application/json");curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{}");curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, stdout);curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 12L);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15L);curl_easy_perform(curl);curl_easy_cleanup(curl);}curl_slist_free_all(list); /* free the list */

只用在上述代码中加入POST的请求体即可,例如curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{}");

更多推荐

互帮互助,小bug记录:libcurl timeout不起作用,程序无法结束

本文发布于:2023-11-15 23:18:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:互帮互助   不起作用   结束   程序   timeout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!