温馨提示×

c++ post能否携带自定义头部

c++
小樊
81
2024-07-05 20:14:20
栏目: 编程语言

是的,C++的Post请求可以携带自定义头部。可以使用HTTP客户端库(如libcurl)来发送HTTP请求,并在请求中包含自定义的头部信息。以下是一个简单的示例代码:

#include <iostream>
#include <curl/curl.h>

int main() {
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
        
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Custom-Header: Value");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        
        res = curl_easy_perform(curl);
        
        curl_slist_free_all(headers);
        
        if (res != CURLE_OK) {
            std::cerr << "Error: " << curl_easy_strerror(res) << std::endl;
        }
        
        curl_easy_cleanup(curl);
    }

    return 0;
}

在上述代码中,我们使用libcurl库发送一个Post请求到"http://example.com",并添加了一个自定义的头部信息"Custom-Header: Value"。您可以根据需要添加更多的自定义头部信息。

0