Completed

Alteração de função get usando Qt (C+

Published on the October 24, 2016 in IT & Programming

About this project

Open

Tenho duas funções (get e post) em um projeto de dll no qt e quero altera-las.

Hoje elas usam a classe QEventLoop para controle da resposta e do timeout e isso tem ocasionado alguns travamentos então quero uma nova abordagem.

Abaixo esta o código a ser alterado.

QByteArray Request::get(
        const QString request,
        const uint32_t timeoutMs,
        const QString authUser,
        const QString authPass,
        QMap<QString, QString> customHeaders)
{
    QNetworkAccessManager manager;
    QTimer timer;
    timer.setSingleShot(true);

    QString auth = authUser + ":" + authPass;

    QEventLoop *loop = new QEventLoop();
    QNetworkRequest req;
    req.setRawHeader("Authorization", "Basic " + auth.toLocal8Bit().toBase64());
    for (QString headerName : customHeaders.keys()) {
        req.setRawHeader(headerName.toLocal8Bit(), customHeaders[headerName].toLocal8Bit());
    }
    req.setUrl(request);
    Qnetworkreply* reply = manager.get(req);
    loop->connect(reply, signal(finished()), slot(quit()));
    loop->connect(&timer, signal(timeout()), slot(quit()));
    timer.start(timeoutMs);
    loop->exec();

    if (timer.isActive()) {
        timer.stop();
        if (reply->error() != QNetworkReply::NoError) {
            throw NetworkException(reply->errorString().toStdString());
        }

        int rc = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        if (rc >= 200 && rc < 300) {
            return reply->readAll();
        }
        throw NetworkException("HTTP return code = " + QString::number(rc).toStdString());
    } else {
        throw NetworkException("Request timed out");
    }

    return QByteArray("");
}

Category IT & Programming
Subcategory Desktop apps
What is the scope of the project? Small change or bug
Is this a project or a position? Project
I currently have I have specifications
Required availability As needed
Experience in this type of projects Yes (I have managed this kind of project before)
Required platforms Windows

Delivery term: October 25, 2016

Skills needed

Other projects posted by W. L. L.