简体   繁体   English

是否可以使用libcurl禁用HTTP Keep-Alive?

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

I am implementing HTTP client for third-party private API. 我正在为第三方私有API实现HTTP客户端。 This API requires to not use HTTP Keep-Alive. 此API要求不使用HTTP Keep-Alive。 Can libcurl disable Keep-Alive? libcurl可以禁用Keep-Alive吗? Even when I set CURLOPT_FORBID_REUSE , it still sends Connection: Keep-Alive header and confuses server. 即使我设置了CURLOPT_FORBID_REUSE ,它仍会发送Connection: Keep-Alive标头并使服务器混乱。 It even sends this header if I manually set Connection: close header. 如果我手动设置Connection: close标头,它甚至会发送此标头。 In such situation libcurl sends both Connection: Keep-Alive and Connection: close HTTP headers. 在这种情况下, libcurl发送Connection: Keep-AliveConnection: close HTTP头。 Does anyone know how to force libcurl to never reuse connections and send Connection: close header to notify server that it doesn't use connection, or Connection: Keep-Alive is hardcoded in libcurl and can not be altered? 有谁知道如何强制libcurl永远不会重用连接并发送Connection: close标头通知服务器它不使用连接,或者Connection: Keep-Alivelibcurl是硬编码的,不能更改?

This can be achieved with CURLOPT_HTTPHEADER : 这可以通过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);
    }
}

At least it works on my Linux. 至少它适用于我的Linux。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM