Network reply always empty with error : RemoteHostClosedError

Viewed 146

I have a secured URL working with an x509 certificate and I am supposed to receive a JSON as an answer. My certificate is in pfx format (ca certificate, local certificate, key). I added it on the computer and when launching a request on google chrome and internet explorer with the secured URL I have indeed the JSON as an answer.

My problem is when I try to do the same inside my Qt application I do not have any answer from the same URL. My code :

    QSslConfiguration config = QSslConfiguration::defaultConfiguration();
    config.setProtocol(QSsl::TlsV1_2);

    _manager = std::make_unique<QNetworkAccessManager>();
    auto line = "myurl";
    qInfo() << line;

    QNetworkRequest request((QUrl::fromUserInput(line)));
    request.setSslConfiguration(config);

    QEventLoop loop;
    connect(_manager.get(), &QNetworkAccessManager::finished, &loop, 
    &QEventLoop::quit, 
    Qt::DirectConnection);

    connect(_manager.get(), &QNetworkAccessManager::sslErrors, this, 
    &CheckConfController::sslErrors);

    _loginReply = std::unique_ptr<QNetworkReply>(_manager->get(request));

    connect(_loginReply.get(), &QNetworkReply::readyRead,
            this, &CheckConfController::replyFinished);
    connect(_loginReply.get(), &QNetworkReply::finished,
            this, &CheckConfController::_loginReceive);
    loop.exec();

The login receive method :

void CheckConfController::_loginReceive(){
    qInfo() << "status code : " << _loginReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    qInfo() << "error : " << _loginReply->error();
    qInfo() << "error string : " << _loginReply->errorString();
    QByteArray b = _loginReply->readAll();
    qInfo() << "byte : " << b;
}

The SSL error method :

void CheckConfController::sslErrors(QNetworkReply* reply, const QList<QSslError> &errors)
{
    QByteArray b = reply->readAll();
    qInfo() << "reply : " << b;

    for(auto element : errors){
        qInfo() << "ssl error : " << element;

    }
}

This is mu Outpput :

status code: QVariant(Invalid)
error: QNetworkReply::RemoteHostClosedError
err string: "Connection Closed"
byte : ""

SSL error is never called.

I switched my URL line to https://www.google.com and it works fine. I also tried with other urls and i receive my json correctly.

This is my configuration:

Qt version: 5.12.11
compiler: MSVC 2017 64 bits
Computer running the application: Windows 7

Any help would be appreciated. Thank you.

1 Answers

Update

After comments below this answer, I noticed the server that OP is trying to reach has certificate limitations, hence defaulting to system certificates is not an option, because we can't be sure which certificate is used.

Now that we know that, I suggest ensuring that your certificate is somehow preferred, for example try:

QSslConfiguration::setDefaultConfiguration(config);

I mean, at end of OP's original code:

QSslKey key;
QSslCertificate certificate;
QList<QSslCertificate> certChain;

bool imported = QSslCertificate::importPkcs12(&pfxFile, &key, &certificate, &certChain, "mypassphrase");
qInfo() << "imported : " << imported;


QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setCaCertificates(certChain);
config.setLocalCertificate(certificate);
config.setPrivateKey(key);
config.setProtocol(QSsl::TlsV1_2);

Old answer

As comments said:

  • OP no longer adds certificate manually (and adds it to system),
  • Also, OP's code works for other HTTPS URLs.
  • Last but not least, OP's Site has valid certificate, and logs just mention closed connection.

Counting all together, this does not seem to have anything to do with Qt. OP's site simply rejects any JSON request.

Maybe try setting auth header (or, do whatever else the site's requirements are).

I usually use Postman App (to test sites' API and routes).

Related