有什么已知的方法可以将字符串或数据从 html 页面发送到 C++ 应用程序吗?

Is there any known way to send strings or data from a html page to a c++ application?

我是这个领域的新手,只是想知道这种事情是否可行。我似乎找不到太多关于它的信息。我唯一能想到的是 javascript 与 C++ 应用程序通信的后端。

我查看了 C++ 的 Qwidget 工具包,但我在网上找不到任何相关示例。

最终,我试图获取一个 html 页面(使用 node.JS 提供服务)并有一个按钮,单击该按钮时,会将一串数据发送到C++ 应用程序。

我不确定如何在其中实施 POST 或 GET 请求,以及是否可以使用 javascript 后端。

如果有人以前遇到过这种方法,并且可以指出正确的方向,甚至是有用的资源或示例,我将非常感激。

您可以使用 cpprest。只需在本地主机上创建 http 服务器并发送 POSTGETOPTIONS 请求。 它将占用不到百分之一的CPU。

示例:

httpserver.h

#ifndef HTTPSERVER_H
#define HTTPSERVER_H

#include <cpprest/producerconsumerstream.h>
#include <cpprest/http_listener.h>
#include <cpprest/json.h>
#include <iphlpapi.h>
#include <Assert.h>
#include <iostream>
#include <iomanip>
#include <sstream>

#pragma comment(lib, "iphlpapi.lib")

using namespace std;
using namespace web;
using namespace http;
using namespace utility;
using namespace concurrency;
using namespace http::experimental::listener;

class HttpServer : public http_listener
{

public:
    HttpServer();
    ~HttpServer();

    //http request
private:
    void HandleGet(http_request request);
    void HandlePost(http_request request);
    void HandleOptions(http_request request);

    json::value ProcessRequest(json::value task);

private:
    //user info
    wstring data;
};


#endif

httpserver.cpp

#include "HttpServer.h"

HttpServer::HttpServer()
    : http_listener(L"http://localhost:9999"s)
{
    data= L"";

    //setting up the server
    this->support(methods::GET, bind(&HttpServer::HandleGet, this, placeholders::_1));
    this->support(methods::POST, bind(&HttpServer::HandlePost, this, placeholders::_1));
    this->support(methods::OPTIONS, bind(&HttpServer::HandleOptions, this, placeholders::_1));

}

HttpServer::~HttpServer()
{this->close();}

void HttpServer::HandleGet(http_request request)
{
    json::value jsonResponse(L"Get response from C++");
    http_response response(status_codes::OK);
    response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
    response.set_body(jsonResponse);
    request.reply(response);
}

void HttpServer::HandlePost(http_request request)
{
    json::value jsonResponse;
    request.extract_json().then([&jsonResponse, this, &request](pplx::task<json::value> task)
    {
        jsonResponse = this->ProcessRequest(task.get());
    }).wait();

    //write status into http response
    http_response response(status_codes::OK);

    //if you need to add CORS into headers
    response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
    response.set_body(jsonResponse);
    request.reply(response);
}

void HttpServer::HandleOptions(http_request request)
{
    http_response response(status_codes::OK);
    response.headers().add(U("Allow"), U("GET, POST, OPTIONS"));
    response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
    response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, OPTIONS"));
    response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
    request.reply(response);

    return;
}

json::value HttpServer::ProcessRequest(json::value task)
{
        for (auto iterator = task.as_object().cbegin(); iterator != task.as_object().cend(); ++iterator)
        {
            const wstring& key = iterator->first;
            const json::value& value = iterator->second;

            if (key == L"data")
            {
                data = value.as_string();
                continue;
            }

            //if you need to send response
            response[L"response"] = json::value::boolean(true);
        }

    return response;
}

main.cpp

int main(int argc, char* argv[])
{
    ::ShowWindow(::GetConsoleWindow(), SW_HIDE);

    unique_ptr<HttpServer> webServer = make_unique<HttpServer>();
    //listen 9999 port
    webServer->open().wait();

    while(true)
        this_thread::sleep_for(chrono::seconds(1));  //Simulate some work by sleeping

    return 0;
}

在客户端:

function requestREST(request/*json data*/,onSuccess/*callback with json response*/) {    
    $.ajax({     
        type: "POST",
        url: "...",
        data: JSON.stringify(request),
        dataType: 'json',
        crossDomain: true,
        contentType: "application/json",
        success: function (response) {
            onSuccess(response);
        },
        timeout:3000,
        statusCode: {
            400: function (response) {
                alert('Not working!');
            },
            0: function (response) {
                alert('Not working!');
            }              
        }
    });