Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt How can i send https request to server?

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();
like image 478
Sarkhan Avatar asked Dec 07 '13 13:12

Sarkhan


1 Answers

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();
}
like image 189
Gábor Angyal Avatar answered Sep 24 '22 18:09

Gábor Angyal