qt 调用Restful服务接口,支持ssl

    QNetworkRequest request;
    QSslConfiguration sslConfig;
    sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
    sslConfig.setProtocol(QSsl::TlsV1SslV3);
    request.setSslConfiguration(sslConfig);

    request.setUrl(QUrl(resCmd->methodName));
    request.setRawHeader("Content-Type", "application/json;charset=UTF-8");
    request.setRawHeader("CLIENTVERSION", ("V1"));
    //???????????????
    if (manager->networkAccessible() == QNetworkAccessManager::NotAccessible)
        manager->setNetworkAccessible(QNetworkAccessManager::Accessible);
    QNetworkReply* reply = manager->post(request, resCmd->data.toUtf8());
    QEventLoop loop;
    QTimer timer;
    timer.setSingleShot(true);
    //??????????
    connect(reply, &QNetworkReply::finished,
        [&timer, &loop]()
    {
        LOG(INFO) << "Rest call finished.";
        if (timer.isActive())
            timer.stop();
        loop.quit(); });
    connect(&timer, &QTimer::timeout, reply, &QNetworkReply::abort);
    timer.start(restReqTimeOut);
    //QNetwork???????????????????????????????
    loop.exec();
    resCmd->responseTime = CURRENT_DATE_TIME_STRING;
    QNetworkReply::NetworkError error = reply->error();
    QString replyMsg;
    if (error != QNetworkReply::NoError)
    {
        QMetaEnum m = QMetaEnum::fromType();
        const char* strError = m.valueToKey(error);
        replyMsg = QString("{\"flag\":false, \"errorCode\":\"%1\", \"message\":\"QNetworkReply error.\"}").arg(strError);
        LOG(ERROR) << replyMsg.toStdString();
    }
    else
    {
        replyMsg = reply->readAll();
    }

你可能感兴趣的:(restful,后端)