Get the html's source from a https url with QNetworkAccessManager
-
Hello,
Now, all web's cite is secured with https protocol.
For a personal automatic's search on the web, i need to get some areas under a html code.
So, with no secured connection, no problemo.
But, the best answers aren't in these ones.A short code to explain is better than my poor english.
#include <QWidget> #include <QNetworkAccessManager> #include <QNetworkReply> QNetworkAccessManager *manager; manager = new QNetworkAccessManager(this); QNetworkRequest request; request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); request.setUrl(QUrl("https://forum.qt.io/topic/95745/get-the-html-s-source-from-a-https-url-with-qnetworkaccessmanager")); request.setRawHeader("User-Agent", "MyOwnBrowser 1.0"); QNetworkReply *response; response = manager->get(request); connect(response, &QNetworkReply::finished,this,&Form::getResult ); void form2::getResult() { qDebug() << "https://forum.qt.io/category/10/general-and-desktop"; qDebug() << "html "<< response->readAll(); ui->label_2->setText("one think done"); }
In some discussions, i can read about SSL's configuration and it said to add it under the compiler command's line. I put mine.
qmake.exe "xyz.pro" -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug" && E:/Qt/Tools/mingw530_32/bin/mingw32-make.exe qmake_all
No idea how to install the Ssl configuration in the mingw32 compiler if it needed.
I earded about redirection which it not applied in this code.
Some helps Please.
Thanks for reading.Ps: surpise works with https://forum.qt.io
So try with request.setUrl(QUrl("https://www.google.fr/search?q=qt+ssl+configuration")); -
You don't have do anything for HTTPS. You should just use the HTTPS url and it should work fine.
What is your real issue ? What is not working ?
-
see my Debug window
Starting xxxx.exe... libpng warning: iCCP: known incorrect sRGB profile url QUrl("https://www.google.fr/search?q=qt+ssl+configuration&oq=Ssl+Configuration+qt") html "" // from response->readAll(); redirect QUrl("") // from qDebug() << "redirect "<< response->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); one think done
-
@An-other-french said in Get the html's source from a https url with QNetworkAccessManager:
QNetworkReply *response;
response = manager->get(request);
try:
reponse->ignoreSslErrors(); -
No way to show the html code from a google search
insert libeay32.dll and ssleay32.dll in the folder "Qt\5.9.4\mingw53_32\bin" -
http://doc.qt.io/qt-5/qnetworkreply.html#ignoreSslErrors-1
void Form::on_pushButton_4_clicked() { manager = new QNetworkAccessManager(this); QNetworkRequest request; request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); QList<QSslCertificate> cert = QSslCertificate::fromPath(QLatin1String("server-certificate.pem")); QSslError error(QSslError::SelfSignedCertificate, cert.at(0)); QList<QSslError> expectedSslErrors; expectedSslErrors.append(error); request.setUrl(QUrl("https://www.google.fr/search?q=qt+ssl+configuration&oq=Ssl+Configuration+qt")); response = manager->get(request); connect(response, &QNetworkReply::finished,this,&Form::getResult2 ); response->ignoreSslErrors(expectedSslErrors); }
an error :
ASSERT failure in QList<T>::at: "index out of range", file E:\Qt\5.9.4\mingw53_32\include/QtCore/qlist.h, line 541 C:/.../Test QT Projet/build-test2-Desktop_Qt_5_9_4_MinGW_32bit-Debug/debug/test2.exe exited with code 3
-
@An-other-french said in Get the html's source from a https url with QNetworkAccessManager:
QList<QSslCertificate> cert = QSslCertificate::fromPath(QLatin1String("server-certificate.pem"));
You should check if there is an entry before accessing it...