Erik Šťastný
Erik Šťastný

Reputation: 1486

QNetworkReply - Strange error in enum, OperationCanceled instead of Timeout?

My API server is turned off and i run following code. I dont understand why QNetworkReply::OperationCanceledError error enum is returned instead of QNetworkReply::TimeoutError. What is wrong? Am i doing something wrong or is it Qt bug?

From documentation that error should be if "the operation was canceled via calls to abort() or close() before it was finished." I see no reason for that.

QByteArray encodedData = data.toUtf8();
QUrl url("http://myapi/jsonrpc");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

QNetworkAccessManager manager;
manager.setTransferTimeout(500);
QNetworkReply* reply = manager.post(request, encodedData);

QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

if (reply->error() != QNetworkReply::NoError) {
    QString errorMsg = QString("HTTP Network request has failed. Code: ") + QVariant::fromValue(reply->error()).toString();
    delete reply;
    // error
    // here i got QNetworkReply::OperationCanceledError
}

QByteArray response = reply->readAll();
//ok

Upvotes: 0

Views: 165

Answers (1)

nils
nils

Reputation: 410

You may find the answer here. It seems that QT itself reacts to the timeout and calls abort() to close the connection. You therefore receive the OperationCanceledError error.

Upvotes: 0

Related Questions