M
M
Maxim Kuprashevich2014-11-10 22:22:27
Qt
Maxim Kuprashevich, 2014-11-10 22:22:27

Why did QtNetwork stop working after moving to another PC?

Hello. I have such a bug, I can not overcome for a couple of days. In short, the program loads the dll and executes the code in it. In the case of my bug, this is a dll containing a request to the server. Everything worked well until I decided to test transfer to another computer. The version of Windows there is the same (I stuck it on a virtual machine), but clean, from scratch. When I transferred, suddenly the request stopped working, the program began to return always an empty response. I was very surprised, I googled for a long time, put everything in a row: Open SSL, Windows SDK, MS VC ++ of all versions, but nothing helped, the answer is empty and the request does not go to the server at all. I got an error and it showed "SSL handshake failed". I don't really understand why, how to fix it and why it works on the old system.
Below is the code and logs obtained from the new system, where it does not work. Tell me please,

const char *lastError = NULL;
const char *URL_TOKEN = "https://blablabla";
const char *URL_AUTH = "https://blablabla=%1";

#define EXT(code) if (!lastError) qDebug() << lastError; return code;
#define SETLERR(msg) if (!lastError) lastError = msg;

// +
RESULT Run(const char *k, void *p)
{
    qDebug() << __FUNCTION__ ;
    (void*) p;
    // Check args
    if ((void*)k == (void*)0)
    {
        SETLERR("(void*)k == (void*)0) is true; Ошибка - отсутствует ключ");
        EXT(RESULT_WRONGARGS);
    }

    QString key(k);
    if (key.isEmpty())
    {
        SETLERR("Ошибка - ключ пустой");
        EXT(RESULT_WRONGKEY);
    }

    // Start working
    qDebug() << "Before QString reply = PostRequest(key);";
    QString reply = PostRequest(key);
    qDebug() << "After QString reply = PostRequest(key);";

    if (reply.isEmpty())
    {
        SETLERR("Ответ от сервера пустой, это может быть вызвано внутренней "
                "ошибкой PostRequest()");
        EXT(RESULT_UNKNOWN_ERROR);
    }
    else if (reply == "GoodTry!")
    {
        SETLERR("Ответ от сервера \"GoodTry\"");
        EXT(RESULT_WRONGKEY);
    }

    QString token_url(URL_AUTH);
    token_url = token_url.arg(reply);
    bool success = QDesktopServices::openUrl(token_url);
    if (success)
    {
        EXT(RESULT_SUCCESS);
    }
    else
    {
        SETLERR("OpenUrl is false, failure;"
                "Ошибка - открыть страницу не удалось");
        EXT(RESULT_FAILURE);
    }

    SETLERR("Amazing error, nobody knows what there have been");
    EXT(RESULT_UNKNOWN_ERROR);
}

// +
const char *GetPluginName()
{
    return "Portal Biocad";
}

// +
const char *GetLastError()
{
    if (!lastError)
        return "no error";
    return lastError;
}

// +-
bool IsAvailable(const char *key)
{
    (void*) key;

    /*
    if (key.empty()) {
        return 0;
    }
    QString reply = PostRequest("");
    if (reply.isEmpty()) {
        return 0;
    }
    else {
        return 1;
    }//*/

    return true;
}

// +
QString PostRequest(const QString &key)
{
    // setting POST request params
    QUrl params;
    params.addQueryItem("key", key);
    // creating request
    QNetworkRequest request;
    request.setUrl(QString(URL_TOKEN));
    request.setHeader(QNetworkRequest::ContentTypeHeader,
                      QVariant("application/x-www-form-urlencoded"));

    // executing request
    QNetworkAccessManager manager;
    QEventLoop wait_reply;

    QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),
                     &wait_reply, SLOT(quit()));
    QNetworkReply *answer = manager.post(request, params.encodedQuery());

    qDebug() << "Before wait_reply.exec();";
    wait_reply.exec();
    qDebug() << "After wait_reply.exec();";

    if (answer)
    {
        if (answer->error() == QNetworkReply::NoError)
        {
            const int available = answer->bytesAvailable();
            if (available > 0)
            {
                return answer->readAll();
            }
        }
        else
        {
            qDebug() <<  QString("Error: %1 status: %2").arg(
                             answer->errorString(),
                             answer->attribute(
                                 QNetworkRequest::HttpStatusCodeAttribute)
                             .toString());
        }

        qDebug() << "code: " << answer->attribute(
                        QNetworkRequest::
                        HttpStatusCodeAttribute).toString()
                 << " response: " << answer->readAll();
        answer->deleteLater();
    }

    return "";
}

Logs:
Run
Before QString reply = PostRequest(key);
Before wait_reply.exec();
"Error: SSL handshake failed status: "
code: "" response: ""
After wait_reply.exec();
After QString reply = PostRequest(key);
Reply is empty

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DancingOnWater, 2014-11-11
@DancingOnWater

Most likely problems with the certificate. My code in a similar situation:

void SpaceTrackConnection::sendReguest(const QString &request) const
{
     QNetworkRequest req(request);
     req.setSslConfiguration(d->m_sslConfiguration);
         req.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/x-www-form-urlencoded"));
         req.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue<QList<QNetworkCookie>>(d->m_cookieList));
         req.setHeader(QNetworkRequest::SetCookieHeader,QVariant::fromValue<QList<QNetworkCookie>>(d->m_cookieList));
    auto reply = d->m_networkAcess->get(req);
     QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(read()));
         QObject::connect(reply, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(errorSlotSSL(const QList<QSslError> &)));
         QObject::connect(reply, SIGNAL(readyRead()), this, SLOT(read()));
         QObject::connect(reply, SIGNAL(finished()), this, SLOT(read()));
}


void SpaceTrackConnection::errorSlotSSL(const QList<QSslError> &erros)
{
    auto sender = static_cast<QNetworkReply*>(this->sender());
    sender->ignoreSslErrors(erros);
    qDebug()<<"ssl errors";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question