如何将SSL证书添加到我的代码中 [英] How do I add a SSL certificate to my code
本文介绍了如何将SSL证书添加到我的代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < QCoreApplication >
#include < QDebug < span class =code-keyword>>
#include < span class =code-keyword>< QNetworkAccessManager >
#include < QNetworkRequest >
#include < QNetworkReply >
#include < QUrl < span class =code-keyword>>
#include < span class =code-keyword>< QUrlQuery >
#include < QFile >
#include < QSslSocket >
void s endRequest();
int main( int argc, char * argv [])
{
QCoreApplication a(argc,argv);
sendRequest();
return a.exec();
}
void sendRequest(){
QList< QSslCertificate> certs = QSslCertificate :: fromPath( C:/Users/mkovacev/Desktop/Work/alipay_cert.pem 的);
// 在堆栈上创建自定义临时事件循环
QEventLoop事件循环;
// quit()事件循环,当网络请求完成()
QNetworkAccessManager mgr;
QObject :: connect(& mgr,SIGNAL(finished(QNetworkReply *)),& eventLoop,SLOT(quit()));
// HTTP请求
QNetworkRequest req(QUrl( QString( https://mapi.alipay.com/gateway.do/_input_charset=UTF-8¤cy=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(); // 阻止堆栈,直到调用finished()
< span class =code-keyword> if (reply-> error()== QNetworkReply :: NoError){
// 成功
qDebug()<< 成功<< reply-> readAll();
删除回复;
}
其他 {
// 失败
qDebug()<< 失败<< reply-> errorString();
删除回复;
}
}
我的尝试:
QList< qsslcertificate> certs = QSslCertificate :: fromPath(C:/Users/mkovacev/Desktop/Work/alipay_cert.pem);
解决方案
如果你想将它作为文件,它应该在应用程序的目录中。对于这种情况,你必须将它包含在你的设置/安装程序中。
艰难的方式(它是不可见的而不是可变的)是将它作为二进制文件包含在你的app中。这是一个资源加载示例。
#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(){
QList<QSslCertificate> certs = QSslCertificate::fromPath("C:/Users/mkovacev/Desktop/Work/alipay_cert.pem");
// 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;
}
}
What I have tried:
QList<qsslcertificate> certs = QSslCertificate::fromPath("C:/Users/mkovacev/Desktop/Work/alipay_cert.pem");
解决方案
If you want it as file it should be in the directory of the app. For that case you must include it your setup/installer.
The hard way (it is invisible and not changeble) is to include it as a binary in you app. Here is an example for resource loading.
这篇关于如何将SSL证书添加到我的代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文