如何解决是否有任何已知的方法可以将字符串或数据从 html 页面发送到 C++ 应用程序?
我是这个领域的新手,只是想知道这种事情是否可行。我似乎无法找到太多关于它的信息。我唯一能想到的就是与 C++ 应用程序通信的 javascript 后端。
我查看了 C++ 的 Qwidget 工具包,但在网上找不到任何示例。
最终,我正在尝试获取一个 html 页面(使用 node.JS 提供服务)并创建一个按钮,单击该按钮后,会将一串数据发送到 C++ 应用程序。
我不确定如何在其中实施 POST 或 GET 请求,如果使用 javascript 后端是可行的方法。
如果有人以前遇到过这种方法,并且可以为我指明正确的方向,甚至是有用的资源或示例,我将不胜感激。
解决方法
您可以使用 cpprest。只需在本地主机上创建 http 服务器并发送 POST、GET 或 OPTIONS 请求。 它将占用不到百分之一的 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,placeholders::_1));
this->support(methods::OPTIONS,bind(&HttpServer::HandleOptions,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,&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"),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!');
}
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。