I tried to send get request with https url. Reply is empty but there is no error message. I download OpenSSL and copied libeay32.dll and ssleay32.dll files to C:\Qt\Qt5.1.1\Tools\QtCreator\bin folder.
Code:
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest request;
QNetworkReply *reply = NULL;
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setProtocol(QSsl::TlsV1_2);
request.setSslConfiguration(config);
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ServerHeader, "application/json");
reply = manager->get(request);
qDebug() << reply->readAll();
As Frank wrote in his commet, the get function is asynchronous, so at the point you are trying to read the answer the http request might not even be completed yet.
To solve this, you need to handle the finished
signal:
connect(manager, SIGNAL(finished(QNetworkReply*)), this,
SLOT(replyFinished(QNetworkReply*)));
and read the results in the handler:
void NetworkHandler::replyFinished(QNetworkReply *reply)
{
qDebug() << reply->readAll();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With