Qt - 从网址下载数据

时间:2017-07-04 14:13:16

标签: qt qurl

我用PHP编写了一个Web服务。我需要通过Web服务URL下载内容。如何从URL下载数据?

需要哪些库?

1 个答案:

答案 0 :(得分:5)

  1. 在.pro项目文件中添加QT += network

  2. 在您的代码中包含以下库:

    #include <QUrl>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QtNetwork/QNetworkRequest>
    #include <QtNetwork/QNetworkReply>
    
  3. 最后通过以下代码接收网址内容:

    QString url = "WEB_SERVICE_URL";
    QNetworkAccessManager manager;
    QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
    QEventLoop event;
    connect(response, SIGNAL(finished()), &event, SLOT(quit()));
    event.exec();
    QString content = response->readAll();