不起作用,程序无法结束"/>
互帮互助,小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不起作用,程序无法结束
发布评论