QT5.9.9开启SSL支持,请求HTTPS链接

软件开发大郭
0 评论
/
4 阅读
/
4614 字
18 2025-01
分类:

简单的处理方法: 在网络上搜索

libeay32.dll
ssleay32.dll

复制到

C:\Qt\Qt5.9.9\5.9.9\mingw53_32\bin

文件夹下

测试代码: SDKClinet.h头文件:

#ifndef SDKCLIENT_H
#define SDKCLIENT_H
#include <qstring.h>
#include <QObject>
#include "httprequestworker.h"

#include <QNetworkAccessManager>
#include <QFile>
#include <QSsl>
#include <QSslKey>
#include <QSslSocket>
#include <QSslConfiguration>
#include <QSslError>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QtNetwork>

class SDKClient: public QObject
{
        Q_OBJECT
public:
    ~SDKClient();
    static SDKClient *Instance();
    explicit SDKClient(QObject *parent = 0);
    bool Login(QString username,QString password);
    bool test();
private:

    static SDKClient *_self;
    QNetworkAccessManager * manager;//网络链接管理器

public slots:
    void getMD5(QNetworkReply *res);
    void handleLoginResponse(HttpRequestWorker* worker);
};

#endif // SDKCLIENT_H

CPP实现核心部分:

SDKClient::SDKClient(QObject *parent) : QObject(parent)
{
    this->manager=new QNetworkAccessManager(this);
    return;
}
SDKClient::~SDKClient(){
    delete(this->manager);
}
bool SDKClient::test(){

    if(false==QSslSocket::supportsSsl()){
        qDebug()<<"不支持SSL";
        return false;
    }
    qDebug()<<"支持SSL";
    QSslConfiguration SSLConfig;
    SSLConfig =QSslConfiguration::defaultConfiguration();
    SSLConfig.setPeerVerifyMode(QSslSocket::QueryPeer);
    SSLConfig.setProtocol(QSsl::TlsV1_2);

    QNetworkRequest request;
    request.setUrl(QUrl("https://blog.grabbyte.com/admin/login"));

    request.setSslConfiguration(SSLConfig);
    manager->get(request);
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(getMD5(QNetworkReply*)));
}

void SDKClient::getMD5(QNetworkReply *res){
    QByteArray bytes=res->readAll();
    if(bytes.isNull()){
        qDebug()<<"获取数据为NULL";
        disconnect(manager,SIGNAL(finish(QNetworkReply*)),this,SLOT(getMD5(QNetworkReply*)));
        return;
    }
    qDebug()<<"getMD5 ok."<<bytes;

    QJsonParseError jsonError;
    QJsonDocument document=QJsonDocument::fromJson(bytes,&jsonError);
    if(jsonError.error!=QJsonParseError::NoError){
        qDebug()<<QStringLiteral("JSON解析失败")<<jsonError.error;
        disconnect(manager,SIGNAL(finish(QNetworkReply*)),this,SLOT(getMD5(QNetworkReply*)));
        return;
    }
    //json 解析
    if(document.isObject()){
        QJsonObject obj=document.object();
        QJsonValue value;
        if(obj.contains("result")){
            value=obj.take("result");
            if(value.isString()){
                QString data=value.toString();
                qDebug()<<data;

            }

        }
    }
    disconnect(manager,SIGNAL(finish(QNetworkReply*)),this,SLOT(getMD5(QNetworkReply*)));
    res->deleteLater();

}

编译自己的dll

下载编译辅助软件perl

https://www.activestate.com/platform/supported-languages/perl/

下载OpenSSL源码

检测需要的版本

bool SDKClient::Login(QString username,QString password){
    QString url_str = "https://blog.grabbyte.com/admin/login";

    HttpRequestInput input(url_str, "POST");

    input.add_var("username", username);
    input.add_var("password", username);

//    HttpRequestWorker *worker = new HttpRequestWorker(this);
//    connect(worker, SIGNAL(on_execution_finished(HttpRequestWorker*)), this, SLOT(handleLoginResponse(HttpRequestWorker*)));
//    worker->execute(&input);



    QNetworkAccessManager manager;
    bool bSupp = QSslSocket::supportsSsl();
    QString buildVersion = QSslSocket::sslLibraryBuildVersionString();
    QString version = QSslSocket::sslLibraryVersionString();
    qDebug() << bSupp << buildVersion << version << endl;

    qDebug() << QStringLiteral("支持发送数据:") << manager.supportedSchemes();



}

下载合适版本的源码

https://slproweb.com/products/Win32OpenSSL.html

标签:
    暂无数据