Results 1 to 3 of 3

Thread: QtNetwork and QHttp->Problem with reading the response header

  1. #1
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QtNetwork and QHttp->Problem with reading the response header

    Hi..
    I have a problem related to a get request in QHttp. I never programmed anything that has to do with network (only PHP and HTML, but that doesn't count), so I'm completely new on this topic..

    I'm trying to send a get request to the local apacha and then read the response code. The server gets the request and the html file gets saved to downloaded_file.txt. So that's working.
    But I want to know the status code of the server, and that's not working.

    Here is the code:

    header file
    Qt Code:
    1. class NetWorkTest : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. NetWorkTest(QWidget *parent = 0);
    6. ~NetWorkTest();
    7.  
    8. protected slots:
    9. void sendHttpRequest();
    10. void act_requestStarted(int p_requestId);
    11. void act_requestFinished(int p_requestId, bool p_er);
    12. void act_readResponseHeader(const QHttpResponseHeader& p_resp);
    13.  
    14. protected:
    15. QLayout* m_layout;
    16. QHttp* m_http;
    17. QFile* m_file;
    18.  
    19. bool m_httpRequestAborted;
    20. int m_httpGetID;
    21. };
    To copy to clipboard, switch view to plain text mode 

    cpp file
    Qt Code:
    1. NetWorkTest::NetWorkTest(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. m_file = new QFile("downloaded_file.txt");
    5. m_file->open(QIODevice::WriteOnly);
    6.  
    7. m_http = new QHttp(this);
    8.  
    9. connect(button, SIGNAL(clicked()), this, SLOT(sendHttpRequest()));
    10. connect(m_http, SIGNAL(requestStarted(int)), this, SLOT(act_requestStarted(int)));
    11. connect(m_http, SIGNAL(requestFinished(int, bool)), this, SLOT(act_requestFinished(int, bool)));
    12. connect(m_http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(act_requestFinished(const QHttpResponseHeader&)));
    13.  
    14. m_http->setHost("127.0.0.1", 8000);
    15. }
    16.  
    17. void NetWorkTest::sendHttpRequest()
    18. {
    19. m_httpGetID = m_http->get("/test.html", m_file);
    20. cout << "Requesting " << m_httpGetID << endl;
    21. }
    22.  
    23. void NetWorkTest::act_requestStarted(int p_requestId)
    24. {
    25. cout << "Request " << p_requestId << " started." << endl;
    26. }
    27.  
    28. void NetWorkTest::act_requestFinished(int p_requestId, bool p_er)
    29. {
    30. cout << "Request " << p_requestId << " finished";
    31. if (p_er) cout << " with an error. (" << m_http->errorString().toStdString() << ")" << endl;
    32. else cout << "." << endl;
    33. }
    34.  
    35. void NetWorkTest::act_readResponseHeader(const QHttpResponseHeader& p_resp) // never got this signal..
    36. {
    37. if(p_resp.statusCode() != 200) {
    38. cout << "Server error " << p_resp.statusCode() << " (" << p_resp.reasonPhrase().toStdString() << ")" << endl;
    39. }
    40. else cout << "All good.." << endl;
    41. m_http->abort();
    42. }
    To copy to clipboard, switch view to plain text mode 

    I've also appended the source, in case you want something to compile..

    Regards..
    aMan..
    Attached Files Attached Files
    Last edited by aMan; 7th September 2006 at 23:57.

  2. #2
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtNetwork and QHttp->Problem with reading the response header

    In your code responseHeaderReceived() signal is connected to act_requestFinished() slot, not to act_readResponseHeader() slot. act_readResponseHeader() is never called.
    The Wheel weaves as the Wheel wills.

  3. #3
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtNetwork and QHttp->Problem with reading the response header

    God, such a stupid mistake..
    I've checked the parameter of the response slot a hundred times, but never the name..

    But it works now..
    Thank you..

    aMan..

Bookmarks

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.