QNetworkAccessManager错误代码99;只在某些系统上......

时间:2015-01-19 23:48:25

标签: c++ qt ssl openssl qnetworkaccessmanager

使用QNetworkAccessManager时,我收到一个奇怪的错误,只发生在某些系统上而不是其他系统上。

请参阅下面的代码。下面的测试连接到基于SSL的站点。

我在与我的应用程序相同的目录中的客户端系统上有OpenSSL DLL( libeay32.dll ssleay32.dll ),但仍然会出现错误。

错误代码为(99) - 未知网络错误。

是什么原因导致某些系统出现此错误而非其他系统?

我该怎么办?

感谢您的时间。

在应用程序的构造函数中:     MainNetConnector =新的QNetworkAccessManager(this);

开始测试的功能:

void QTBasicWidget::performNetworkTest() {

    QMessageBox::StandardButton RequestNetworkTest;

    RequestNetworkTest = QMessageBox::information(this, "Would you like to initiate a network test?",
        "Would you like to perform a network test?\n\nThe application will test the network for connectivity to and from the licensing server specified.\n\nResults will be displayed in the 'Log' tab.",
        QMessageBox::Yes | QMessageBox::No);

    QNetworkRequest req;

    QByteArray postData;

    QNetworkReply* reply;

    switch (RequestNetworkTest) {

    case QMessageBox::Yes:

        LoggingWidget->logText(tr("Beginning the network test."));

        if (UseSecuredURL) {

            req.setUrl(QUrl(SecureURL + "testconnection.php"));

            LoggingWidget->logText(tr("Connecting to secure URL: ") + SecureURL + tr("testconnection.php"));

        }
        else {

            req.setUrl(QUrl(UnsecureURL + "testconnection.php"));

            LoggingWidget->logText(tr("Connecting to unsecure URL: ") + UnsecureURL + tr("testconnection.php"));

        }

        MainNetConnector->setCookieJar(new QNetworkCookieJar(MainNetConnector));

        LoggingWidget->logText(tr("Cookie jar prepared for data to send."));

        reply = MainNetConnector->post(req, postData);

        LoggingWidget->logText(tr("Data sent."));

        connect(reply, &QNetworkReply::finished, this, &QTBasicWidget::processTestResults);
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
            this, SLOT(networkError(QNetworkReply::NetworkError)));

        if (TestingTimer) {

            TestingTimer->stop();
            TestingTimer->deleteLater();
            TestingTimer = NULL;

        }

        TestingTimer = new QTimer(this);
        connect(TestingTimer, SIGNAL(timeout()), this, SLOT(onDelayedNetworkTestReponse()));
        TestingTimer->start(5000);

        break;

    case QMessageBox::No:

        QMessageBox::information(this, "Test cancelled.",
            "Network test cancelled.",
            QMessageBox::Ok);

        break;

    default:

        QMessageBox::information(this, "Test cancelled.",
            "Network test cancelled.",
            QMessageBox::Ok);

        break;

    }

}

收到测试结果的函数:

void QTBasicWidget::processTestResults() {

    if (TestingTimer) {

        TestingTimer->stop();
        TestingTimer->deleteLater();
        TestingTimer = NULL;

    }

    LoggingWidget->logText(tr("Reply received."));
    auto reply = qobject_cast< QNetworkReply *>(sender());
    QByteArray bytes = reply->readAll();
    QString str = QString::fromUtf8(bytes.data(), bytes.size());

    LoggingWidget->logText(tr("Raw data from reply: ") + str);

    GLint statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

    LoggingWidget->logText(tr("Reply status code: ") + QString::number(statusCode));

    if (statusCode == 200) {

        QMessageBox::StandardButton NetworkTestResults;

        NetworkTestResults = QMessageBox::information(this, "Test succeeded!.",
            "Network test succeeded.",
            QMessageBox::Ok);

    }
    else {

        QMessageBox::StandardButton NetworkTestFailed;

        NetworkTestFailed = QMessageBox::critical(this, "Test failed!.",
            "Network test failed.",
            QMessageBox::Ok);

    }

}

错误处理功能:     void QTBasicWidget :: networkError(QNetworkReply :: NetworkError err){

    if (CurrentlyDebugging) {

        LoggingWidget->logText(tr("Network error. Error code is: ") + QString::number(err));

    }

    QMessageBox::StandardButton CriticalMessageBox;

    CriticalMessageBox = QMessageBox::critical(this, tr("Licensing Server Error"),
        tr("Specific error code was (") + QString::number(err) + tr(")"), QMessageBox::Ok);

}

2 个答案:

答案 0 :(得分:1)

http://doc.qt.io/qt-5/qtnetwork-downloadmanager-example.html

http://doc.qt.io/qt-5/qtnetwork-download-example.html

看起来您可能也应检查SSL错误。

希望这是一个一致的错误,你可以找到正在发生的事情。我最好的猜测是它与SSL有关。下载程序示例显示了如何查看这些错误。

#ifndef QT_NO_SSL
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>)));
#endif

// ...

void DownloadManager::sslErrors(const QList<QSslError> &sslErrors)
{
#ifndef QT_NO_SSL
    foreach (const QSslError &error, sslErrors)
        fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
#else
    Q_UNUSED(sslErrors);
#endif
}

希望有所帮助。

答案 1 :(得分:0)

使用“ C:\ Qt \ MaintenanceTool.exe” 添加或删除组件 在Qt中 >开发人员和设计师工具 > OpenSSL x.y.z工具包

从EXE文件目录中的以下目录复制所有DLL。 C:\ Qt \ Tools \ OpenSSL \ Win_x64 \ bin