如何在QWebPage之前从特定URL的QNetworkReply
响应中读取数据? 但是当发出finished()
信号时,QWebPage已经读取了回复,因此连接readyRead()
或调用reply->readAll()
什么也没返回。我在自己的QWebPage类中尝试了重载acceptNavigationRequest((方法,如下所示:
bool webPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
{
//qDebug() << "filename = " << request.rawHeader("content-disposition");
if(request.url().path() == QStringLiteral("download.php"))
{
QNetworkReply *reply = networkAccessManager()->get(request);
QFile file;
file.setFileName(filename);
if(!file.open(QIODevice::ReadWrite))
{
/* handle error */
}
file.write(reply->readAll());
file.close();
return false;
}
但是我无法回复工作...返回的回复无效(甚至不返回 http 状态代码,我知道这意味着发送的 http 请求无效,但我不知道为什么(。
欢迎使用不同的方法来解决此问题!
使用带有 lambda 表达式的完成插槽,您可以执行以下操作: -
QNetworkReply* reply = networkAccessManager()->get(request);
connect(reply, &QNetworkReply::finished, [=]() {
if(reply->error() == QNetworkReply::NoError)
{
QByteArray response = reply->readAll();
// do something with the data...
}
else // handle error
{
qDebug(pReply->errorString());
}
});