PDA

View Full Version : QNetworkReply: How to get content in case of error? (status != 200)



SteveXP
3rd September 2020, 18:50
Hello,

my problem is i don't get the error message in case something went wrong. In case QNetworkReply::error() != QNetworkReply::NoError all fields are empty. Just
QNetworkReply::errorString provides "Connection closed" or sometimes "Connection refused". But with Wireshark i can see the device/server does indeed send more information including the reason for the "Connection closed".
How can i get the statuscode or description?





MyClass::MyClass(QObject* parent)
{
m_pNetManager = new QNetworkAccessManager(this);
connect(m_pNetManager, &QNetworkAccessManager::finished, this, &MyClass::readReply);
}

MyClass::readReply(QNetworkReply* const pReply)
{
if (pReply->error() != QNetworkReply::NoError)
{
qDebug() << pReply->readAll();
qDebug() << pReply->rawHeaderList();
qDebug() << pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute );
qDebug() << pReply->attribute(QNetworkRequest::RedirectionTargetAttrib ute);
qDebug() << pReply->errorString() << pReply->error();

pReply->deleteLater();
return;
}
// normal program flow here ...
}


Output of above code when receiving an error:


""
()
QVariant(Invalid)
QVariant(Invalid)
"Connection closed" QNetworkReply::NetworkError(RemoteHostClosedError)


On WireShark there are 3 TCP packages [ACK][PSH,ACK][FIN,ACK], the second contains:


HTTP/1.1 401 Unauthorized
WWW-Authenticate: Digest realm="AXIS_WS_ACCC8E7913BD", nonce="oPoxc2iuBQA=1d8e93903510c2de354fadda8b313cd6da6372 dc", algorithm=MD5, qop="auth"
Server: gSOAP/2.7
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 620
Connection: close

It seems i only get the content of messages which wiresharks marks as HTTP/XML-Protocol. The above 401 Unauthorized is only in a TCP-Package.
How do i get the "401 Unauthorized"?

Thanks for any ideas.

ChrisW67
5th September 2020, 05:34
If that is the entire response from the server then you are getting the appropriate error message. The Content-Length header indicates there should be substantial content following the headers (620 bytes) but the server is terminating the connection before this has been received. That is, this is not a valid/complete HTTP response and Qt is not interpreting it as such.

By my reading, if you received the entire HTTP response then line 11 would show those 620 bytes from the body, line 13 would give QNetworkReply::AuthenticationRequiredError (or QNetworkReply::ContentAccessDenied), and line 15 the text "Unauthorised". Your QNetworkAccessManager would probably emit authenticationRequired() as well.