gpt4 book ai didi

node.js - 有错误代码时 QNetworkReply readAll 响应为空

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:07 25 4
gpt4 key购买 nike

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

我有一个 Node+Express 服务器,当 http 状态不同于 200 时,它总是发送详细描述;我无法从 QNAM 上的 Qt 客户端获取此描述。 Qt版本为5.3,操作系统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();
}

非常感谢您的帮助。

最佳答案

来自documentation :

QByteArray QIODevice::readAll()

This is an overloaded function.

Reads all available data from the device, and returns it as a QByteArray.

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.

关于node.js - 有错误代码时 QNetworkReply readAll 响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25734247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com