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 &开发者_JAVA技巧; 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 );
}
}
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);
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)));
}
Here is how I ended up resolving my issue: https://bitbucket.org/libki-kms/libki-client/src/917e8201c997a9f37b57420c0df68c8751527f2a/networkclient.cpp?at=master
Thanks for help all!
精彩评论