2017-01-23 68 views
0

我有问题,在下面的代码中,“QSslCertificate :: fromPath”找不到我指定的文件,但是当我检查它时使用下面的fileExists函数,它告诉我该文件毕竟存在。只有当我尝试在与我的开发PC不同的PC上运行我的应用程序时,才会出现此问题。我在Windows 10 64 Bit上开发,测试PC是Windows 7 64 Bit。我使用QT 5.4.0。我究竟做错了什么?Qt:QSslCertificate :: fromPath没有找到我的文件,但QFileInfo发现它

void MyClass::init() 
{ 

    // ... some other init code 

    QLatin1String rootCApath = QLatin1String("./ssl/rootCA.crt"); 
    if (fileExists(rootCApath)) 
     log("File exists"); // This is just a log function for displaying messages in the GUI. 
    else 
     log("File doesn't exist"); 

    static QList<QSslCertificate> cert = QSslCertificate::fromPath(rootCApath); 
    if(cert.size()==1) 
    { 
     ssl_configuration.setCaCertificates(cert); 
     m_webSocket.setSslConfiguration(ssl_configuration); 
    } 
    else 
    { 
     QString s = "Server certificate not found. Size is " + QString::number(cert.size()); 
     log(s); 
    } 
} 

bool MyClass::fileExists(QString path) { 
    QFileInfo check_file(path); 
    // check if file exists and if yes: Is it really a file and no directory? 
    return check_file.exists() && check_file.isFile(); 
} 

编辑:当我阅读证书到一个QByteArray中,并传递一个,那么我的连接功能不Windows 7的PC机上做任何事情,而在我的Windows 10开发PC一切仍然正常工作。

此外,还有另一台PC始终给我TLS握手错误,无论我做什么。 3台电脑,3个结果,令人沮丧。

回答

0

我发现我的情况的解决方案:我没有在那里添加OpenSSL库LIBEAY32.DLL和ssleay32.dll到目录我exe文件驻留在。

0

你的代码看起来很好,在我的linux系统上工作没有任何问题。您应该检查相对路径或简单地尝试通过QFileInfo设置证书:

QLatin1String rootCApath = QLatin1String("./ssl/rootCA.crt"); 
QFileInfo temp(rootCApath); 
QString tempssl; 
QDir dir ;//try to set dir also if this does not work dir("/home/foo/yourapppath/"); 

tempssl=dir.relativeFilePath("./ssl/rootCA.crt"); 
static QList<QSslCertificate> cert = QSslCertificate::fromPath(tempssl); 

if(cert.size()==1) 
{ 
    ssl_configuration.setCaCertificates(cert); 
    m_webSocket.setSslConfiguration(ssl_configuration); 
} 
else 
{ 
    QString s = "Server certificate not found. Size is " + QString::number(cert.size()); 
    log(s); 
} 
+0

不幸的是,这并没有帮助,无论我是否将QDir dir设置为QCoreApplication :: applicationDirPath()。 – Alex

相关问题