Calling REST services with Qt4

2019-02-20 05:22发布

Can anyone point me to a simple way to make calls to a REST service using Qt4? I cannot find any decent documentation on how to do this. Right now I have something like:

int NetworkClient::attemptLogin( QString username, QString password, int & error ) {

  QNetworkAccessManager *manager = new QNetworkAccessManager(this);

  QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(slotRequestFinished(QNetworkReply *)));

  QNetworkRequest request;
  request.setUrl(QUrl("http://192.168.20.155:3000/api/rest/user?id=" + username));
  request.setHeader(QNetworkRequest::ContentTypeHeader, "text/xml");

  QNetworkReply *reply = 0;
  reply = manager->get(request);

  qDebug() << reply;

Am I even close?

Here is my calling code:

void LoginWindow::attemptLogin() {
  int loginError;

  QString username = usernameField->text();
  QString password = passwordField->text();

  int minutes = net.attemptLogin( username, password, loginError );

  if ( minutes ) {
    attemptLoginSuccess( username, password, minutes );
  } else {
    attemptLoginFailure( loginError );
  }
}

标签: qt rest qt4
3条回答
唯我独甜
2楼-- · 2019-02-20 05:58

Here's my take...

QNetworkReply* Gateway::performPost(QString service, QMap<QString, QString>params)
{
    qDebug() << QString("Post with params #").append(service) << params;

    //Initialize uri
    QString uri = Gateway::SERVICE_BASE_URI;
    QNetworkAccessManager oManager;

    //Connect signal
    connect(oManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));

    //Construct request
    QUrl res = QUrl(
        uri.append(service)
    );

    QUrl query;

    QMapIterator<QString, QString> i(params);
    while(i.hasNext()) {
        i.next();
        query.addEncodedQueryItem(QUrl::toPercentEncoding(i.key()), QUrl::toPercentEncoding(i.value()));
    }

    //Do request
    QNetworkRequest postRequest = QNetworkRequest(res);

    //Return QNetworkReply Object   
    return oManager.post(postRequest, query.encodedQuery());
}


//Implementation
QMap<QString,QString> map;

map.insert("foo", "bar");
map.insert("bar", "foo");

//Perform the POST request
oGateway.performPost("foo/bar", map);
查看更多
聊天终结者
3楼-- · 2019-02-20 05:59

Your function is close to what I use when calling REST services. What I do is create one function for each http verb.

This is my post function (note the support for SSL and Basic Authorization)

void Http::post(QString &url, QString &user, QString &password, QString &data)
{
    QSslConfiguration config(QSslConfiguration::defaultConfiguration());

    QNetworkRequest request;


    request.setSslConfiguration(config);

    request.setRawHeader("Authorization", "Basic " +
                         QByteArray(QString("%1:%2").arg(user).arg(password).toAscii().toBase64())
                     );
    request.setRawHeader("Accept", "application/xml");
    request.setRawHeader("Content-Type", "application/xml");

    request.setUrl(QUrl(url));

    qDebug() << url;

    connect(netManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));

    reply = netManager->post(request, data.toAscii());

    connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(progress(qint64,qint64)));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslError(QList<QSslError>)));
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(networkError(QNetworkReply::NetworkError)));
}
查看更多
劫难
4楼-- · 2019-02-20 06:02
登录 后发表回答