lundi 20 avril 2015

Downloading file over HTTPS using Qt behind authenticating proxy

I am trying to download a file, publicly available over an HTTPS URL, using Qt 4.8. I am behind a corporate authenticating proxy (over which I have no control).

I am using QNetworkAccessManager, as shown below:

bool FileDownloader::download(const QString& fileUrl, const QString& fileName)
{
    QString outputFileName = fileName.isEmpty() ? fileNameFromUrl(fileUrl) : fileName;

    _file.setFileName(outputFileName);
    _file.open(QIODevice::WriteOnly);

    QUrl url(fileUrl);

    QNetworkReply* pReply = _networkAccessManager.get(QNetworkRequest(url));
    pReply->ignoreSslErrors();

    connect(pReply, SIGNAL(finished()), _pEventLoop, SLOT(quit()));
    connect(pReply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    connect(pReply, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(onSslErrors(const QList<QSslError>&)));
    connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));

    _pEventLoop->exec();

    return true;
}

As soon as I call QNetworkAccessManager::get(), and receive a QNetworkReply object, I connect to its sslErrors signal as mentioned in the docs. Following the same docs, I'm also calling ignoreSslErrors() in the slot connected to that signal:

void FileDownloader::onSslErrors(const QList<QSslError>& sslErrors)
{
    QNetworkReply* pReply = (QNetworkReply*)(sender());
    pReply->ignoreSslErrors(sslErrors);
}

However, I never receive an sslErrors signal, I always get an error signal, which I'm handling in the onError slot, and which always says:

SSL handshake failed

I'm calling the download functionality thus:

FileDownloader fileDownloader;
fileDownloader.setProxy("http://username:password@proxyserver:8080");
fileDownloader.download(
        "http://ift.tt/1JmLPd7",
        "f:\\temp\\test.png"

I do not want to use other libraries like libcurl because I don't want to introduce dependencies.

So how can I download the file over HTTPS using Qt?

Aucun commentaire:

Enregistrer un commentaire