如何从 QT C++ 中的 Web 服务获取 XML 中的响应

How to get response in XML from web service in QT c++

我正在尝试为 Web 服务创建客户端应用程序。在该应用程序中,我需要通过该 Web 服务 link 传递一些整数,然后 Web 服务以 XML 格式给我一些数据。我能够完成 ping 该 Web 服务,但我不知道如何使用该 Web 服务传递整数值。请给我一些用 C++ 编写的请求-响应 Web 服务的示例代码。响应数据在 XML 中,我想存储在一些文本文件或向量中。我需要使用打印机打印的数据。

这是我为 Web 服务开发的代码:

void Downloader::doDownload()
{
    manager = new QNetworkAccessManager(this);
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("http://api.flickr.com/services/soap/")));
    exit(0);
}
void Downloader::replyFinished (QNetworkReply *reply)
{
    if(reply->error())
    {
      qDebug() << "ERROR!!!!";
      qDebug() << reply->errorString();
    }
    else
    {
        qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
        qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
        qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
        qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();

        QFile *file = new QFile("/root/Downloads/QT Web Services/WebServiceResponseData.txt");
        if(file->open(QFile::Append))
        {
        file->write(reply->readAll());
        file->flush();
        file->close();
        }
        delete file;
    }

    reply->deleteLater();
}

我可能错了,但您可能需要传递 POST 或 GET 参数才能传递您的号码。

获取:

GET 将要求您在 URL 末尾添加参数,如下所示:http://yoururl.com/page?arg1=1&arg2=2&arg3=3

//Replace "arg" by the argument name provided by the API documentation, something like "?xml"
QString urlString = QString("http://api.flickr.com/services/soap?arg=%1").args(QString::number(yourNumber))
manager->get(QNetworkRequest(QUrl(urlString)));

POST :

POST 将要求您使用 QUrlQuery 对象添加所需的参数

QUrlQuery *postData = new QUrlQuery;
//Replace "arg" by the argument name provided by the API documentation, something like "xml"
postData->addQueryItem("arg", yourNumber);
manager->post(QNetworkRequest(QUrl("http://api.flickr.com/services/soap/")), postData->toString(QUrl::FullyEncoded).toUtf8());

您需要肥皂吗?

为此使用 KDSoap:https://github.com/KDAB/KDSoap

*.wsdl有和没有代码生成的示例:https://github.com/KDAB/KDSoap/tree/master/examples