如何使用 POCO 库将 C++ 对象传输到 Web 服务

How to transfer a C++ object to a web service using POCO library

我有一个使用 Qt 和 openCV 的图像处理应用程序。

对于每一帧,我应该将捕获的cv::Mat图像对象发送到服务器进行处理并得到结果。

我应该使用 REST 架构,因为它的播放负载很低。

我应该使用什么工具来将 cv::Mat 发送到服务器。

为了便携性,我正在使用 POCO。

我正在寻求最轻的解决方案,我需要服务器每秒处理 10 帧的最低速度。

我的意思是,有没有一种方法可以在不显式序列化的情况下将 C++ 对象传递给服务器?

编辑

有了POCO库,你可以看看这个答案:。他正在 ifstream.

上发送文件

在此答案中,您可以查看如何将 cv::Mat 转换为 istream

最后,由于多态性,istream 被隐式转换为 ifstream。


您可以使用 C++ Rest SDK。 PUT 命令的代码示例。

Source of code

Library Github where you can find the full documentation.

#include <http_client.h>
#include <filestream.h>
#include <iostream>
#include <sstream>

using namespace web::http;
using namespace web::http::client;

// Upload a file to an HTTP server.
pplx::task<void> UploadFileToHttpServerAsync()
{
    using concurrency::streams::file_stream;
    using concurrency::streams::basic_istream;

    // To run this example, you must have a file named myfile.txt in the current folder. 
    // Alternatively, you can use the following code to create a stream from a text string. 
    // std::string s("abcdefg");
    // auto ss = concurrency::streams::stringstream::open_istream(s); 

    // Open stream to file. 
    return file_stream<unsigned char>::open_istream(L"myfile.txt").then([](pplx::task<basic_istream<unsigned char>> previousTask)
    {
        try
        {
            auto fileStream = previousTask.get();

            // Make HTTP request with the file stream as the body.
            http_client client(L"http://www.fourthcoffee.com");
            return client.request(methods::PUT, L"myfile", fileStream).then([fileStream](pplx::task<http_response> previousTask)
            {
                fileStream.close();

                std::wostringstream ss;
                try
                {
                    auto response = previousTask.get();
                    ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;
                }
                catch (const http_exception& e)
                {
                    ss << e.what() << std::endl;
                }
                std::wcout << ss.str();
            });
        }
        catch (const std::system_error& e)
        {
            std::wostringstream ss;
            ss << e.what() << std::endl;
            std::wcout << ss.str();

            // Return an empty task. 
            return pplx::task_from_result();
        }
    });

    /* Sample output:
    The request must be resent
    */
}