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

标签 c++ web-services rest opencv poco-libraries

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

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

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

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

我正在使用 POCO 来实现便携性。

我寻求最轻的解决方案来做到这一点,我需要服务器每秒处理 10 帧的最低速度。

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

最佳答案

编辑

使用 POCO 库,您可以查看此答案:HttpRequest PUT content in poco library .他正在 ifstream 上发送文件。

在这个答案中,您可以查看如何将 cv::Mat 转换为 istream:OpenCV cv::Mat to std::ifstream for base64 encoding .

最后,由于多态性,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
    */
}

关于c++ - 如何使用 POCO 库将 C++ 对象传输到 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36771435/

相关文章:

c++ - 无法在运行时打开 Code::Blocks 应用程序中的任何文件

java - Wsdl 地址位置显示主机名而不是本地主机

javascript - ($根作用域 :inprog) $apply already in progress

ASP.Net Web API - 授权 header 空白

java - 如何让 Swagger UI 的参数成为下拉菜单而不是文本输入

c++ - 当使用 "%x"打印 numeric_limits<float>::infinity() 时会发生什么?

c++ - 在创建结构时修改类私有(private)成员 (C++)

c++ - 过早的优化还是我疯了?

Eclipse 在使用已部署的 WAR 文件启动时自动打开 Web 浏览器

java - 错误 :org. ksoap2.SoapFault 服务类对象没有实现需要的方法