简体   繁体   English

如何让Qt 5.8在Windows上使用OpenSSL

[英]How to get Qt 5.8 working with OpenSSL on Windows

I am trying to get Qt 5.8 working with OpenSSL on Windows, but every time I get a step forward I hit another bigger object. 我试图让Qt 5.8在Windows上使用OpenSSL,但每次我向前迈出一步,我都会遇到另一个更大的对象。

Edit/Update: This error occurs only in debug mode! 编辑/更新:此错误仅在调试模式下发生!

Here is my setup so far: 这是我到目前为止的设置:

  • Installed Qt 5.8 安装Qt 5.8
  • Downloaded and compiled version of OpenSSL 已下载和编译的OpenSSL版本
  • Transfer the small test project to MSVS2015 将小型测试项目转移到MSVS2015
  • Copied dll files (libeay32.dll and ssleay32.dll) to the application Directory 将dll文件(libeay32.dll和ssleay32.dll)复制到应用程序目录
  • SSL connection is now working fine SSL连接现在正常工作

But here is the problem: Every time I had a connection open whether with SSL or not I get an error by quitting the application. 但问题是:每当我打开连接时是否使用SSL,我都会因退出应用程序而出错。 Exception thrown at (ntdll.dll). 抛出异常(ntdll.dll)。

错误消息,调用堆栈,...

I tested my code with the Visual Leak Detector because I thought it was a Memory issue, but it does not solve my Problem. 我使用Visual Leak Detector测试了我的代码,因为我认为这是一个内存问题,但它并没有解决我的问题。 I rally have no clue where I should start anymore... 我不知道我应该在哪里开始......

Here is a small example in Qt Creator 4.2.1 which doesn't work either 这是Qt Creator 4.2.1中的一个小例子,它也不起作用

Here is my Code (it works fine with http, when I delete the OpenSSL dll's): 这是我的代码(当我删除OpenSSL dll时,它适用于http):

void InfoGatherer::getInfo(QString name)
{
    // TODO: Search for the name and select right page
    QUrl url = QUrl(name);
    data.clear();

    QNetworkRequest *request = new QNetworkRequest(url);
    request->setRawHeader("User-Agent", userAgent);

    if (name.startsWith("https"))
    {
        QSslConfiguration sslConfiguration(QSslConfiguration::defaultConfiguration());
        sslConfiguration.setProtocol(QSsl::TlsV1_2OrLater);
        request->setSslConfiguration(sslConfiguration);
    }
    else
    {
        // TODO: Try to get https
    }

    reply = webCtrl->get(*request);

    connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
    connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));

    // Mem leaks (Visual Leak Detector)
    if(!name.startsWith("https"))
        webCtrl->deleteResource(*request);

    delete request;
}

void InfoGatherer::onReadyRead()
{
    data.append(reply->readAll());
}

void InfoGatherer::slotError(QNetworkReply::NetworkError)
{
    // TODO
    qWarning() << "ErrorNo: " << reply->error() << "for url: " << reply->url().toString();
    qDebug() << "Request failed, " << reply->errorString();
    qDebug() << "Headers:" << reply->rawHeaderList() << "content:" << reply->readAll();
}

void InfoGatherer::onReplyFinished()
{
    QString html = QString(data);

    emit got_webpage(&html);
}

Qt detects the OpenSSL library correctly: Qt正确检测OpenSSL库:

qDebug() << "Support SSL:  " << QSslSocket::supportsSsl()
        << "\nLib Version Number: " << QSslSocket::sslLibraryVersionNumber()
        << "\nLib Version String: " << QSslSocket::sslLibraryVersionString()
        << "\nLib Build Version Number: " << QSslSocket::sslLibraryBuildVersionNumber()
        << "\nLib Build Version String: " << QSslSocket::sslLibraryBuildVersionString();
  • Support SSL: true 支持SSL:true
  • Lib Version Number: 268443791 Lib版本号:268443791
  • Lib Version String: "OpenSSL 1.0.2h 3 May 2016" Lib Version字符串:“OpenSSL 1.0.2h 2016年5月3日”
  • Lib Build Version Number: 268443791 Lib Build版本号:268443791
  • Lib Build Version String: "OpenSSL 1.0.2h 3 May 2016" Lib Build Version String:“OpenSSL 1.0.2h 2016年5月3日”

Maybe you could try the prebuild Windows binaries for OpenSSL from slproweb.com . 也许你可以从slproweb.com尝试为OpenSSL 构建Windows二进制文件。 Also it seems that Qt 5.8 needs OpenSSL v1.0.2 to work properly. 此外,似乎Qt 5.8需要OpenSSL v1.0.2才能正常工作。

Solution: 解:

Build all dll's from OpenSSL again and dont forget to build the debug dll's. 再次从OpenSSL构建所有dll,不要忘记构建调试dll。

Here is a very good link how to build the dll's. 是一个如何构建dll的非常好的链接。 Build the 64 Bit normal and debug Version! 构建64位正常和调试版本!

From the Footnotes use: - nmake -f ms\\nt dll .mak (install) to get the dll's 从脚注使用: - nmake -f ms \\ nt dll .mak(install)获取dll的

And don't rename dll's to libeay64.dll and ssleay64.dll. 并且不要将dll重命名为libeay64.dll和ssleay64.dll。

This works for me. 这适合我。 Just a local variable for request. 只是请求的局部变量。

QUrl imageUrl = QUrl (link);
QNetworkRequest request (imageUrl);

/* SSL Configuration */
QSslConfiguration sslConfiguration = request.sslConfiguration();
sslConfiguration.setPeerVerifyMode (QSslSocket::VerifyNone);
sslConfiguration.setProtocol (QSsl::AnyProtocol);
request.setSslConfiguration (sslConfiguration);
networkAccessMgr->get (request);
  • Verify Peer Mode: QSslSocket::VerifyNone 验证对等模式: QSslSocket::VerifyNone
  • Protocol: QSsl::AnyProtocol 协议: QSsl::AnyProtocol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM