我想做的是发送一个 https 请求到 https://mapi.alipay.com/gateway.do?在 QT 中并得到响应。每当我 发送请求我收到以下错误。
qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method qt.network.ssl: QSslSocket: cannot call unresolved function SSL_CTX_new qt.network.ssl: QSslSocket: cannot call unresolved function SSL_library_init qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error Failure "Error creating SSL context ()"
我被告知我需要一个 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¤cy=USD¬ify_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;
}
}
最佳答案
要向您的 Qt 程序添加额外的 SSL 证书,您可以使用:
QSslSocket::addDefaultCaCertificates("example.pem");
但是,我认为这并不能解决您的任何错误。您问题中的错误与缺少 SSL 证书无关,而是与缺少 SSL 功能有关。您可能正在使用过时的 SSL 库? (例如,Windows 上 libeay32.dll 和 ssleay32.dll 的旧版本或不兼容版本?)
关于c++ - 我如何在 QT 中将 SSL 证书添加到我的代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36748409/