我试图登录网页,然后下载只有会员可以下载的一些数据。
我已执行发布请求,然后执行 get 请求,由于某种原因 get 请求首先完成。< / p>
有没有办法等待帖子完成或先完成?
编辑:
void downloader::LoginToHost()
{
QNetworkRequest request(QUrl("http://www.example.com/login.php"));
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
QByteArray data("email=example&password=");
manager->post(request,data);
}
void downloader::Do_Request(QUrl url)
{
manager->get(QNetworkRequest(url));
}
void downloader::finished(QNetworkReply * reply)
{
QMessageBox mess;
if(reply->error() == QNetworkReply::NoError)
{
std::ofstream source("source-code.txt");
QString content = reply->readAll();
source << content.toStdString() << std::endl;
source.close();
mess.setText("Done download!");
}
else mess.setText(reply->errorString());
mess.exec();
}
答案 0 :(得分:2)
发送帖子,在帖子返回之前不发送get,并在QNetworkReply中触发finished信号。
假设有两个请求,一个用于帖子,一个用于get,你已经设置了,这里是你可以做的概述: -
QNetworkRequst postRequest;
QNetworkRequest getRequest;
发送postRequest返回QNetworkReply ...
QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager;
QNetworkReply postReply = networkAccessManager->post(postRequest, someData);
QNetworkReply getReply = nullptr; // C++ 11 nullptr
然后您可以连接到postReply完成的信号: -
// using a connection to a lambda function, send the get request when post has finished
connect(postReply, &QNetworkReply::finished, [this, networkAccessManager, getRequest, getReply](){
// handle the reply...
...
// send the getRequest
getReply = networkAccessManager->get(getRequest);
});