在 Qt 中发出 HTTP 请求

发布于 2024-10-03 07:10:49 字数 462 浏览 13 评论 0原文

我是Qt新手。我安装了 Qt for VS2008 并与我的 VS2010 集成。我只是想知道如何发出 HTTP 请求。我读过有关 QtNetwork 的内容,但 QtHttp 已过时。

我还了解 libcurlcurlpp,但我在安装它以及使其与 Qt 一起工作时遇到问题。

您推荐什么,QtNetwork 还是 curlpp?如果QtNetwork,您能给我一个示例函数或一段代码(以及使用什么类)。如果curlpp(libcurl),您能否指出我可以找到为Qt安装它的步骤的地方(或者请解释一下)?

非常感谢。

I'm new to Qt. I installed Qt for VS2008 and integrated with my VS2010. I just want to know how to make HTTP requests. I've read about QtNetwork but QtHttp is obselete.

I also know about libcurl and curlpp, but I have problems installing it, and making it work with Qt.

What do you recommend, QtNetwork or curlpp? If QtNetwork, can you please give me a sample function or piece of code (and what class to use). If curlpp(libcurl), can you please point me to somewhere where I can find the steps to install it for Qt (or kindly explain)?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-10-10 07:10:49

libcurlcurlpp 是很棒的库,但使用它们会增加项目的依赖关系,而您可能可以避免这种依赖关系。

最新版本的 Qt 建议使用 QNetworkAccessManager 来发出网络请求(包括http请求)并接收回复。

下载文件最简单的方法是:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));

当调用 replyFinished 槽时,QNetworkReply 作为参数的对象将包含下载的数据以及元数据(标头等)。

更完整的示例可以在 Qt 示例中找到,您可以在这里阅读其源代码代码

libcurl and curlpp are great libraries, but using them adds a dependency to your project that probably you can avoid.

Recent versions of Qt recommend to use QNetworkAccessManager to make network requests (included http requests) and receive replies.

The simplest possible way to download a file is:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));

When the replyFinished slot is called, the QNetworkReply object that it takes as parameter will contain the downloaded data as well as meta-data (headers, etc.).

A more complete example can be found in the Qt examples, you can read here its source code.

财迷小姐 2024-10-10 07:10:49

Giuseppe 是对的,您不需要使用 libcurlcurlpp 和类似的库。
没有必要,Qt 自己有一个简单且工作的类。

请记住,发送请求和检索回复的标准方式是异步的。
您始终必须将管理器完成(QNetworkReply*)信号连接到插槽。

如果您发送多个请求并且不想为每个回复添加一个槽,您始终可以运行事件循环,并将管理器信号连接到事件循环 quit() 槽。

像这样的东西:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QEventLoop *eventLoop = new QEventLoop();
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), eventLoop, SLOT(quit());

manager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));
eventLoop->exec(QEventLoop::ExcludeUserInputEvents);

QByteArray replyData = reply->readAll();
... //do what you want with the data your receive from reply

顺便说一句。不知道你在做什么。但如果它是一个移动应用程序,我建议你从 VS 切换到 QtCreator IDE。它有一个很好的模拟器和一个完整的移动设备测试工具链。

Giuseppe is right, you don't need to use libcurl, curlpp and similar libraries.
There is no need for that, Qt has a simple and working class on it own.

Keep in mind that the standard way of sending request and retrieving reply is asynchronous.
You always have to connect the manager finished(QNetworkReply*) signal to a slot.

If you send multiple requests and don't want to add a slot for each reply, you can always run an event loop, and connect the managers signal to event loops quit() slot.

Something like this:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QEventLoop *eventLoop = new QEventLoop();
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), eventLoop, SLOT(quit());

manager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));
eventLoop->exec(QEventLoop::ExcludeUserInputEvents);

QByteArray replyData = reply->readAll();
... //do what you want with the data your receive from reply

Btw. don't know what are you doing. But if its a mobile app, I would recommend you switch from VS to QtCreator IDE. It has a nice simulator and a complete toolchain for mobile device testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文