如何在QT中为我的代码添加SSL证书

时间:2016-04-20 15:23:10

标签: c++ qt ssl https

我要做的是发送https请求 https://mapi.alipay.com/gateway.do?在QT并获得回复。每当我 发送请求我得到以下错误。

  

qt.network.ssl:QSslSocket:无法调用未解析的函数SSLv23_client_method qt.network.ssl:QSslSocket:无法调用   未解决的函数SSL_CTX_new qt.network.ssl:QSslSocket:不能   调用未解析的函数SSL_library_init qt.network.ssl:QSslSocket:   无法调用未解析的函数ERR_get_error qt.network.ssl:   QSslSocket:无法调用未解析的函数ERR_get_error失败   “创建SSL上下文时出错()

我被告知我需要SSL证书,所以我使用了

openssl s_client -connect mapi.alipay.com:443 -showcerts 

获得证书。但是什么时候

如何在QT中为我的项目代码添加SSL证书?

    #include <QCoreApplication>
    #include <QDebug>
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    #include <QNetworkReply>
    #include <QUrl>
    #include <QUrlQuery>
    #include <QFile>
    #include <QSslSocket>

    void sendRequest();

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        sendRequest();
        return a.exec();
    }
    void sendRequest(){



    // create custom temporary event loop on stack
    QEventLoop eventLoop;

    // "quit()" the event-loop, when the network request "finished()"
    QNetworkAccessManager mgr;
    QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));

    // the HTTP request
    QNetworkRequest req( QUrl( QString("https://mapi.alipay.com/gateway.do/_input_charset=UTF-8&currency=USD&notify_url=10.237.221.84:80&out_trade_no=123456789&partner=2088101122136241&sign=760bdzec6y9goq7ctyx96ezkz78287de&subject=Coke&sign_type=MD5&service=create_forex_trade&total_fee=0.01") ) );
    QNetworkReply *reply = mgr.get(req);
    eventLoop.exec(); // blocks stack until "finished()" has been called

    if (reply->error() == QNetworkReply::NoError) {
        //success
        qDebug() << "Success" <<reply->readAll();
        delete reply;
    }
    else {
        //failure
        qDebug() << "Failure" <<reply->errorString();
        delete reply;
    }
}

1 个答案:

答案 0 :(得分:1)

要向Qt程序添加其他SSL证书,您可以使用:

QSslSocket::addDefaultCaCertificates("example.pem");

但是,我认为这不会解决您的任何错误。您的问题中的错误与缺少的SSL证书无关,但与缺少的SSL功能无关。您可能正在使用过时的SSL库? (例如,在Windows上较旧或不兼容的libeay32.dll和ssleay32.dll版本?)