Results 1 to 2 of 2

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

  1. #1
    Join Date
    Sep 2020
    Thanked 1 Time in 1 Post
    Qt products
    Unix/X11 Windows

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


    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?

    Qt Code:
    1. MyClass::MyClass(QObject* parent)
    2. {
    3. m_pNetManager = new QNetworkAccessManager(this);
    4. connect(m_pNetManager, &QNetworkAccessManager::finished, this, &MyClass::readReply);
    5. }
    7. MyClass::readReply(QNetworkReply* const pReply)
    8. {
    9. if (pReply->error() != QNetworkReply::NoError)
    10. {
    11. qDebug() << pReply->readAll();
    12. qDebug() << pReply->rawHeaderList();
    13. qDebug() << pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    14. qDebug() << pReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    15. qDebug() << pReply->errorString() << pReply->error();
    17. pReply->deleteLater();
    18. return;
    19. }
    20. // normal program flow here ...
    21. }
    To copy to clipboard, switch view to plain text mode 

    Output of above code when receiving an error:
    "Connection closed" QNetworkReply::NetworkError(RemoteHostClosedError)
    On WireShark there are 3 TCP packages [ACK][PSH,ACK][FIN,ACK], the second contains:
    Qt Code:
    1. HTTP/1.1 401 Unauthorized
    2. WWW-Authenticate: Digest realm="AXIS_WS_ACCC8E7913BD", nonce="oPoxc2iuBQA=1d8e93903510c2de354fadda8b313cd6da6372dc", algorithm=MD5, qop="auth"
    3. Server: gSOAP/2.7
    4. Content-Type: application/soap+xml; charset=utf-8
    5. Content-Length: 620
    6. Connection: close
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Mar 2009
    Brisbane, Australia
    Thanked 1,595 Times in 1,523 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows
    Wiki edits

    Default Re: QNetworkReply: How to get content in case of error? (status != 200)

    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.

Similar Threads

  1. Buid error: collect2.exe: error: ld returned 1 exit status
    By Demonlaplce in forum Installation and Deployment
    Replies: 0
    Last Post: 21st April 2016, 10:18
  2. Replies: 4
    Last Post: 14th October 2015, 00:42
  3. Replies: 5
    Last Post: 29th March 2015, 22:31
  4. Replies: 1
    Last Post: 28th December 2012, 23:13
  5. Replies: 11
    Last Post: 10th February 2011, 01:38


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.