是否可以禁用HTTP Keep-Alive与libcurl

Is it possible to disable HTTP Keep-Alive with libcurl?

本文关键字:Keep-Alive libcurl HTTP 是否      更新时间:2023-10-16

我正在为第三方私有API实现HTTP客户端。此API要求不使用HTTP Keep-Alive。libcurl可以禁用Keep-Alive吗?即使当我设置CURLOPT_FORBID_REUSE,它仍然发送Connection: Keep-Alive头和混淆服务器。它甚至发送这个头,如果我手动设置Connection: close头。在这种情况下,libcurl发送Connection: Keep-AliveConnection: close HTTP头。有没有人知道如何强迫libcurl永远不要重用连接,并发送Connection: close头通知服务器,它不使用连接,或者Connection: Keep-Alivelibcurl中硬编码,不能改变?

这可以通过CURLOPT_HTTPHEADER:

实现
#include <curl/curl.h>
int main(void) {
    CURL *curl = curl_easy_init();
    struct curl_slist *list = NULL;
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
        list = curl_slist_append(list, "Connection: close");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
        curl_easy_perform(curl);
        curl_slist_free_all(list);
        curl_easy_cleanup(curl);
    }
}