将 URI 请求添加到 LibCurl Post 命令

问题描述 投票:0回答:1

我正在使用 ubuntu 22.04 和 libcurl 7.81.0 创建和发送 onvif post 命令。我最初让一切都在 python 中工作,但无法在最终项目中使用 python。因此我想在 libcurl 中复制该命令。我的问题不在于发送命令和接收响应。

我需要更清楚地了解如何在标头中设置请求 URI。当我使用 zeep 通过 python 发送 post 命令时,post 标头如下图所示(来自wireshark的图像):

但是,当我尝试在 libcurl 内发送帖子消息时,我收到以下信息:

我是否缺少设置该 URI 字段的curlopt 选项?我真的需要curl来准确复制python中发送的内容。

我的卷曲选项设置为:

curl = curl_easy_init();
std::string url = serverIP + ":" + std::to_string(serverPort);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, settings.responseTimeout_ms);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &cameraResponse);

我的回调只是:

size_t writeCallback(char* contents, size_t size, size_t nmemb, std::string* userp)
{
    userp->append(contents, size * nmemb);
    return size * nmemb;
}

然后我创建并设置我的标题字段(省略本文中的实际字符串内容)

struct curl_slist* header = nullptr;
header = curl_slist_append(header, host.c_str());
header = curl_slist_append(header, agent.c_str());
header = curl_slist_append(header, acceptEncoding.c_str());
header = curl_slist_append(header, accept.c_str());
header = curl_slist_append(header, connection.c_str());
header = curl_slist_append(header, soapAction.c_str());
header = curl_slist_append(header, contentType.c_str());
header = curl_slist_append(header, contentLength.c_str());

内容只是我想要发送的肥皂格式的编码数据。据我所知,这里也没有问题。

然后我设置最后一个卷曲选项并调用 Easy Perform

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
CURLcode curlResult = curl_easy_perform(curl);

我发现使用相机中的 libcurl 的响应速度比使用 python 时慢约 2 倍。我假设这与消息格式的差异有关。我想首先修复上面提到的 URI 请求。希望获得有关该主题的反馈。

此外,我看到我的内容的curl选项是“URLOPT_POSTFIELDS”复数。我假设这只是意味着我在我的情况下塞进内容的任何内容(不是这个卷曲选项的实际额外输入)。

c++ http post libcurl onvif
1个回答
0
投票

您需要在

CURLOPT_URL
中指定完整的URL,而不仅仅是服务器的IP和端口,例如:

std::string url = "http://" + serverIP + ":" + std::to_string(serverPort) + "/onvif/DeviceIO";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
© www.soinside.com 2019 - 2024. All rights reserved.