简体   繁体   English

QNetworkAccessManager:将响应与请求相关联

[英]QNetworkAccessManager: Associate response with request

I try to do a simple GET to a REST Service and want to know how I can identify/associate the request to the response I get in my SLOT function. 我尝试对REST服务进行简单的GET,并想知道如何识别请求/将请求与在SLOT函数中获得的响应相关联。 For example I might send multiple requests the same manager and want to pass a messageID in order to read that ID in my SLOT function to associate the response with the responsible request. 例如,我可能会向同一管理者发送多个请求,并希望传递messageID以便在我的SLOT函数中读取该ID,以将响应与负责的请求相关联。

Any ideas how to do this? 任何想法如何做到这一点?

QNetworkAccessManager manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply)),
this, SLOT(replyFinished(QNetworkReply*)));

manager->get(QNetworkRequest(QUrl("http://qt-project.org")));

Short answere: The reply provides a function that returns the request: QNetworkReply::request() ... 简短的答复:答复提供了一个返回请求的函数: QNetworkReply::request() ...

Try to check the documention first next time. 下次尝试第一次检查文档。

EDIT: 编辑:
If you need more data than that, you have various ways of doing that. 如果您需要更多的数据,则可以采用多种方法。 For example you could add the data as a dynamic property to the request (see QObject::setProperty ) or store the QNetworkReply returned by QNetworkAccessManager::get function inside a QHash with the data you need. 例如,你可以添加数据的动态特性的要求(见QObject::setProperty )或存储QNetworkReply由归国QNetworkAccessManager::get功能的内部QHash你需要的数据。

you could use c++11 bindings: 您可以使用c ++ 11绑定:

QNetworkAccessManager manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply)), this, 
SLOT(replyFinished(QNetworkReply*)));
QNetworkRequest *request = new QNetworkRequest(QUrl("http://qt-project.org"));
QNetworkReply *reply = manager->get(request );
QObject::connect(reply, &QNetworkReply::finished, std::bind(&YOURCLASS::onGetFinished, this, reply, request));

void YOURCLASS::onGetFinished( QNetworkReply* reply, QNetworkRequest* request ) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM