-1

Is there any example that shows QNetworkAccessManager using OpenSSL? The code snippet or link to the article?

Alexander V
  • 8,351
  • 4
  • 38
  • 47
Rampp
  • 1
  • 1
  • 5
  • Welcome to SO! Please do some basic research (Googling, searching previous answers) before asking a question. If you have done this, at least let us know by linking to previous questions that seem similar but did not help, etc. The narrower you can make your question, the better. – jonspaceharper May 17 '16 at 22:01
  • Possible duplicate of [How to Include OpenSSL in a Qt project](http://stackoverflow.com/questions/14681012/how-to-include-openssl-in-a-qt-project) – jonspaceharper May 17 '16 at 22:21
  • Hi , I have made google search but did not find suitable example for me. the reason may be I have no clear idea how to link openssl in Qt and using general ssl within QNetworkAccessMAnager. And thanks for the link provided. I hope it helps me in good way. – Rampp May 18 '16 at 14:41

1 Answers1

1

Is there any example that shows QNetworkAccessManager using OpenSSL? The code snippet or link to the article?

The answer to this question is of two parts:

1) Using SSL in general

2) Linking Qt-based program with OpenSSL

The answer to first question can be this video. And there are plenty of answers on stackoverflow, just not as systematic as that video which mentions QNetworkAccessManager as well. Also, I like this thread: QNetworkRequest and default SSL configuration. Make sure you use either post or get methods of QNetworkAccessManager to send the request out.

The answer to second question can be: How to Include OpenSSL in a Qt project.

I usually do something like:

// connect 'finished' signal with consumer
connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply*)),
        this, SLOT(requestFinished(QNetworkReply*)));

// prepare request and that URL starts with https:// 
QNetworkRequest rqst(QUrl(jsonUser->url()));
QByteArray postData = myJsonDocument().toJson(QJsonDocument::Compact);
rqst.setRawHeader("Content-Type", "application/json");
auto strSize = QByteArray().append(QString::number(postData.size()));
rqst.setRawHeader("Content-Length", strSize);


// send the request out using HTTPS Post
QNetworkReply* pNetworkReply = m_networkAccessManager.post(rqst, postData);

 // connect 'SSL errors' signal with consumer
connect(pNetworkReply, SIGNAL(sslErrors(const QList<QSslError> &)),
        this, SLOT(onSSLError(const QList<QSslError> &)));

And the data is received via the slot void MyClass::requestFinished(QNetworkReply*). Handling SSL errors is critically important and the most frequent ones are about missing/wrong certificates on the client system. Of course all the above is not necessarily in one function but rather represents snippets from functions related to the flow.

Community
  • 1
  • 1
Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • Hi Alexander,Thanks for the video link. It helped me alot and also the sample code you have given. I will try to implement it in my program and will let you know. – Rampp May 18 '16 at 14:43