WebKit (or WebEngine ?) and secured web site download
-
Hi,
I open a pictures catalog web site secured by login in a QWebView. When I clic on the web site link to download a picture, nothing happen.
Before opening the web site in the QWebView, I make this connexion :
connect(ui->webView->page()->networkAccessManager(),SIGNAL(finished(QNetworkReply *)),this,SLOT(onFinished(QNetworkReply *)));
In "onFinished", I take a look at QNetworkReply but it's empty. The web site use a php script to do the download.
After many tests, it seems I receive the "finished" signal before the end of the download. I tried with a delay but I got nothing more.In many forums (and in the documentation), I found methods that work with urls containing the filename to download. In my case, this is the php script which ask server for the file.
Do you have an idea for me ? Thanks
-
Hi,
Still stuck !
I succeed to obtain "reply->rawHeaderList" :
"Date" "Server" "Content-Disposition" "Vary" "Content-Encoding" "Keep-Alive" "Connection" "Transfer-Encoding" "Content-Type"
but I tried to save in a file "reply->readall" and the file was empty.
I tried the "browser" example (which is working since Qt5.5) but download doesn't work in my case. -
Hi I'm desperate... Does anyone can help me ?
Here is my test code :#include "mainwindow.h" #include "ui_mainwindow.h" #include <QBuffer> #include <QFile> #include <QStandardPaths> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); nam=new QNetworkAccessManager(); ui->webView->page()->setNetworkAccessManager(nam); cookies=new MyCookieJar(); nam->setCookieJar(cookies); diskCache = new QNetworkDiskCache(this); QString location = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); diskCache->setCacheDirectory(location); nam->setCache(diskCache); connect(ui->webView->page()->networkAccessManager(),SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinished(QNetworkReply*))); connect(ui->webView->page(), SIGNAL(unsupportedContent(QNetworkReply*)),this, SLOT(onUnsupported(QNetworkReply*))); connect(ui->webView->page(),SIGNAL(statusBarMessage(QString)),ui->statusBar,SLOT(showMessage(QString))); request.setUrl(QUrl("http://www.xxxxxxxxx.xxx/photo/download.php")); reponse=ui->webView->page()->networkAccessManager()->get(request); connect(reponse,SIGNAL(readyRead()),this,SLOT(readytoread())); ui->webView->load(request); m_pBuffer=new QByteArray(); } MainWindow::~MainWindow() { delete m_pBuffer; delete nam; delete ui; } void MainWindow::readytoread() { QNetworkReply *reply=qobject_cast<QNetworkReply*>(sender()); qDebug()<<"Ready to read:"<<reply->bytesAvailable(); } void MainWindow::onFinished(QNetworkReply *reply) { if (reply->error() != QNetworkReply::NoError) { qDebug()<<"Finished!!"<<reply->errorString(); if(reply->open(QIODevice::ReadOnly)) { m_pBuffer->clear(); } connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(DL_Progress(qint64,qint64))); QList<QByteArray> listHeader=reply->rawHeaderList(); for(int i=0;i<listHeader.size();++i) qDebug()<<listHeader[i]<<"->"<<reply->rawHeader(listHeader[i]); QString txtTemp=reply->rawHeader("Content-Disposition"); if(txtTemp.contains("filename")){ qDebug()<<"filename: "<<txtTemp.section('\"',1,1); qDebug()<<"file size="<<reply->readBufferSize(); } qDebug()<<cookies->getAllCookies (); } reply->deleteLater(); } void MainWindow::onUnsupported(QNetworkReply *reply) { qDebug()<<"unsupported! datas size:"<<reply->bytesAvailable(); *m_pBuffer = reply->readAll(); QFile file("test.png"); file.open (QIODevice::WriteOnly); file.write (*m_pBuffer); file.close (); reply->deleteLater(); } void MainWindow::DL_Progress(qint64 recieved, qint64 total) { qDebug()<<"Progress: "<<recieved<<"/"<<total; }
I change the url in the code but you can try with a photo web site with login : this is my problem.
I don't know why but I never succeed to download the complete file: the web page download successfully then I click "Download" link and code produce "onFinished" with "operation canceled" error; download of the file begin because the headers tell it but file is never complete. The best I got is the end of the file.
I hope you can help.
Thanks