You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++ Curl连接VMware vCenter REST API返回404 NotFound问题

问题分析与解决方案

核心问题

你的C++代码中未显式指定使用POST请求,而libcurl默认会发送GET请求。vCenter的/rest/com/vmware/cis/session接口仅支持POST方法,因此GET请求会触发404错误——这就是命令行工具能成功,但代码调用失败的根本原因。

修复步骤

在curl配置代码中添加以下两行,强制使用POST请求:

// 明确指定请求方法为POST
curl_easy_setopt(curl, CURLOPT_POST, 1L);
// 该接口无需请求体,设置为空字符串(部分服务器要求POST必须携带body)
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");

修改后的完整代码片段

bool generateSessionID(std::string &sessionID) {
    CURL *curl;
    CURLcode res = CURLE_OK;
    std::string response;

    std::string hostname = "192.168.0.223"; // Hardcoded vCenter IP address
    std::string username = "administrator@vcenter.com"; // Hardcoded username
    std::string password = "Password1"; // Hardcoded password

    curl = curl_easy_init();
    if (curl) {
        std::string url = "https://" + hostname + "/rest/com/vmware/cis/session";
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
        curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

        // 新增:指定POST请求
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");

        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        } else {
            long http_code = 0;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
            std::cout << "HTTP response code: " << http_code << std::endl;
            std::cout << "Response: " << response << std::endl;

            if (http_code == 200) {
                Json::CharReaderBuilder builder;
                Json::CharReader *reader = builder.newCharReader();
                Json::Value jsonObj;
                std::string errs;

                if (reader->parse(response.c_str(), response.c_str() + response.size(), &jsonObj, &errs)) {
                    sessionID = jsonObj["value"].asString();
                    std::cout << "Session ID: " << sessionID << std::endl;
                } else {
                    std::cerr << "Failed to parse JSON: " << errs << std::endl;
                }

                delete reader;
            } else {
                std::cerr << "Error: HTTP response code " << http_code << std::endl;
            }
        }

        curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
    }

    return res == CURLE_OK;
}

额外验证点

  • 确认WriteCallback函数实现正确,能正常接收响应内容
  • 检查vCenter版本是否支持该REST接口(vCenter 6.5及以上版本均支持)
  • 确保代码拼接的URL无多余空格或非法字符(当前拼接逻辑正常)

内容的提问来源于stack exchange,提问作者andrewk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 15:24:49