在 Qt 中发出 HTTP 请求
我是Qt新手。我安装了 Qt for VS2008 并与我的 VS2010 集成。我只是想知道如何发出 HTTP 请求。我读过有关 QtNetwork 的内容,但 QtHttp 已过时。
我还了解 libcurl 和 curlpp,但我在安装它以及使其与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
libcurl 和 curlpp 是很棒的库,但使用它们会增加项目的依赖关系,而您可能可以避免这种依赖关系。
最新版本的 Qt 建议使用
QNetworkAccessManager
来发出网络请求(包括http请求)并接收回复。下载文件最简单的方法是:
当调用
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:
When the
replyFinished
slot is called, theQNetworkReply
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.
Giuseppe 是对的,您不需要使用 libcurl、curlpp 和类似的库。
没有必要,Qt 自己有一个简单且工作的类。
请记住,发送请求和检索回复的标准方式是异步的。
您始终必须将管理器完成(QNetworkReply*)信号连接到插槽。
如果您发送多个请求并且不想为每个回复添加一个槽,您始终可以运行事件循环,并将管理器信号连接到事件循环 quit() 槽。
像这样的东西:
顺便说一句。不知道你在做什么。但如果它是一个移动应用程序,我建议你从 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:
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.