当出现错误代码时,QNetworkReply readAll响应为空

时间:2014-09-08 22:50:53

标签: node.js qt express qnetworkreply

当QNetworkReply :: error()!= NoError时,QNetworkReply :: readAll的响应为空。 这是正常的吗?

我的Node + Express服务器总是在http状态不同于200的情况下发送详细说明;我无法从QNA客户端基于QNAM获得此描述。 Qt版本是5.3,OS Win 7 64b。

这是我的代码,我真的觉得这不会有帮助。

PendingRequest *Foo::sendMsg(QStandardItem *requestItem, HTTP_METHOD_ID method, QString path)
{
    PendingRequest *pReq = new PendingRequest(method);

    QString url = QString("https://%1:%2%3").arg(host, QString::number(port), path);
    QNetworkRequest qNetReq = QNetworkRequest(QUrl(url));

    //set headears
    qNetReq.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, HttpUserAgent);
    qNetReq.setRawHeader("Connection", "Keep-Alive");
    if(!credentials.isEmpty())
    {
        qNetReq.setRawHeader("Authorization", QByteArray("Basic ")+credentials);
    }
    if(!sessionId.isEmpty())
    {
        qNetReq.setRawHeader("Session-Id", sessionId);
    }

    //send request
    QNetworkReply *reply;
    if(method == HTTP_METHOD_ID::POST)
    {
        qNetReq.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
        QByteArray data = outHandlerList[outHandlerIndex](requestItem);
        reply = netManager.post(qNetReq, data);
    }
    else
    {
        reply = netManager.get(qNetReq);
    }

    connect(reply, SIGNAL(finished()), this, SLOT(handleResponse()));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(handleSslErrors(QList<QSslError>)));

    return pReq;
}

这是我处理回复的地方:

void Foo::handleResponse()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());

    if(reply->hasRawHeader("Session-Id"))
        sessionId = reply->rawHeader("Session-Id");

    PendingRequest *pReq = pendingRequestMap.contains(reply) ? pendingRequestMap.take(reply) : 0;
    Q_ASSERT(pReq);

    QStandardItem *responseItem = 0;
    QString error;

    if(reply->error() != QNetworkReply::NoError)
    {
        qDebug() << "readAll: ", reply->readAll(), "error: ", reply->errorString();
        error = reply->errorString();
    }
    else
    {
        responseItem = inHandlerList[pReq->inHandlerIndex](reply, error, pReq);
    }

    reply->deleteLater();
}

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

来自documentation

  

QByteArray QIODevice :: readAll()

     

这是一个重载功能。

     

从设备读取所有可用数据,并将其作为a返回   的QByteArray。

     

此功能无法报告错误; 返回空   QByteArray()可能意味着 ,或者当前没有数据可用   阅读,或 发生错误