在 C++/Qt(充当服务器)中创建简单的 WebService,提供 JSON 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11558237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating simple WebService in C++ / Qt (acting as server) providing JSON data
提问by Horst Walter
I need to create a simple web service(being the "server"). The goal is to provide some data I do read in an Qt / C++ application as JSONdata. Basically a JavaScript application in the browser shall read its data from the Qt app. It is usually a single user scenario, so the user runs a Google Maps application in her browser, while additional data come from the Qt application.
我需要创建一个简单的 Web 服务(作为“服务器”)。目标是提供一些我在 Qt/C++ 应用程序中读取的数据作为JSON数据。基本上,浏览器中的 JavaScript 应用程序应从 Qt 应用程序读取其数据。通常是单用户场景,因此用户在浏览器中运行 Google Maps 应用程序,而其他数据则来自 Qt 应用程序。
So far I have found these libs:
到目前为止,我已经找到了这些库:
- Qxt: http://libqxt.bitbucket.org/doc/0.6/index.htmlbut being a newbie on C++/Qt I miss some examples. Added: I have found one example here
- gSoap: http://www.cs.fsu.edu/~engelen/soap.htmlhas more examples and documentation and also seems to support JSON
- KD SOAP: http://www.kdab.com/kdab-products/kd-soap/with no example as far as I can tell, docu is here
- Qt features itself, but it is more about acting as a client: http://qt-project.org/videos/watch/qt-networking-web-services
- Qxt:http://libqxt.bitbucket.org/doc/0.6/index.html但作为 C++/Qt 的新手我错过了一些例子。补充:我在这里找到了一个例子
- gSoap:http: //www.cs.fsu.edu/~engelen/soap.html有更多的例子和文档,似乎也支持 JSON
- KD SOAP:http: //www.kdab.com/kdab-products/kd-soap/ 据我所知没有例子,docu 在这里
- Qt 有自己的特点,但更多的是充当客户端:http: //qt-project.org/videos/watch/qt-networking-web-services
Checking SO gives me basically links to the above libs
检查 SO 基本上给了我上面库的链接
- webservice with Qtwith an example I do not really get.
- How to Create a webservice by Qt
- 带有 Qt 的 webservice 和一个我没有真正理解的例子。
- 如何通过 Qt 创建 Web 服务
So basically I do have the following questions:
所以基本上我有以下问题:
- Which lib would you use? I want to keep it as simple as possible and would need an example.
- Is there another (easy!) way to provide the JSON data to the JavaScript Web page besides the WebService?
- 你会使用哪个库?我想让它尽可能简单并且需要一个例子。
- 除了 WebService 之外,还有其他(简单!)方法可以将 JSON 数据提供给 JavaScript 网页吗?
-- Edit, remarks: ---
-- 编辑,备注:---
Needs to be application intrinsic. No web server can be installed, no extra run time can be used. The user just runs the app. Maybe the Qt WebKit could be an approach....
需要是应用程序内在的。不能安装 Web 服务器,也不能使用额外的运行时间。用户只是运行应用程序。也许 Qt WebKit 可能是一种方法......
-- Edit 2 --
-- 编辑 2 --
Currently checking the tiny web servers as of SO " Qt HTTP Server?"
目前正在检查 SO“ Qt HTTP Server?”的微型 Web 服务器。
采纳答案by Horst Walter
As of my tests, currently I am using QtWebApp: http://stefanfrings.de/qtwebapp/index-en.htmlThis is one of the answers of Edit 2 ( Qt HTTP Server?)
在我的测试中,目前我正在使用 QtWebApp:http://stefanfrings.de/qtwebapp/index-en.html这是 Edit 2(Qt HTTP Server?)的答案之一
Stefan's small WebServer has some well documented code, is written in "Qt C++" and easy to use, especially if you have worked with servlets already. Since it can be easily integrated in my Qt project, I'll end up with an internal WebServer.
Stefan 的小型 WebServer 有一些有据可查的代码,用“Qt C++”编写并且易于使用,特别是如果您已经使用过 servlet。因为它可以很容易地集成到我的 Qt 项目中,所以我最终会得到一个内部 WebServer。
Some demo code from my JSON tests, showing that generating the JSON content is basically creating a QString
.
来自我的 JSON 测试的一些演示代码,表明生成 JSON 内容基本上是创建一个QString
.
void WebServiceController::service(HttpRequest& request, HttpResponse& response) {
// set some headers
response.setHeader("Content-Type", "application/json; charset=ISO-8859-1");
response.setCookie(HttpCookie("wsTest","CreateDummyPerson",600));
QString dp = WebServiceController::getDummyPerson();
QByteArray ba = dp.toLocal8Bit();
const char *baChar = ba.data();
response.write(ba);
}
If someone has easy examples with other libs to share, please let me know.
如果有人有其他库的简单示例可以分享,请告诉我。
回答by Stefan
QByteArray ba = dp.toLocal8Bit();
const char *baChar = ba.data();
You don't need to convert the QByteArray
to char array. Response.write()
can also be called with a QByteArray
.
您不需要将 转换QByteArray
为字符数组。Response.write()
也可以用QByteArray
.
By the way: qPrintable(dp)
is a shortcut to convert from QString
to char array.
顺便说一句:qPrintable(dp)
是转换QString
为字符数组的快捷方式。