Results 1 to 3 of 3

Thread: HTTP GET authentication does not proceed

  1. #1
    Join Date
    May 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default HTTP GET authentication does not proceed

    I am trying to authenticate but get the QT "Authentication required" from the QT requestFinished handler. My code outputs the qDebug below. The "Authenication req'd ..." statement gets printed when the QT signal authenticationRequired is fired. So since the authentication signal fired, and can't figure out why I get teh "Authentication required" error.

    When I look at the wireShark trace, I can see the HTTP 401 unauthorized response from the server and then I see the TCP connection close (as it should). But what does not happen is QT creating a new TCP session followed by the second GET - this one having the authentication info (password and username).

    I stepped into the QAuthenticator::setpassword and setuser and I can see the passworkd and user being set.

    The error output is kinda useless.

    I have compared my code to the QT examples and I don't think I am missing anything that would prevent the second TCP session from starting.

    I also have my code below the qDebug output. Any ideas what I am missing?

    Qt Code:
    1. HTTP requestStarted slot handler for request type setHost (id = 1)
    2. HTTP requestFinished handler: error for request setHost, error = No error
    3. HTTP done signal slot handler. No Error.
    4. HTTP requestStarted slot handler for request type GET (id = 2)
    5. HTTP State for request type GET changed to Connecting
    6. HTTP State for request type GET changed to Sending
    7. HTTP State for request type GET changed to Reading
    8. Authentication req'd for host 169.xxx.xxx.xxx, User Name = root, Password = smokey
    9. HTTP requestFinished handler: error for request GET, error = Authentication required
    10. HTTP done signal slot handler. Error: Authentication required
    11. HTTP State for request type changed to Closing
    12. HTTP State for request type changed to Unconnected
    To copy to clipboard, switch view to plain text mode 



    My cpp file. The run() is at the bottom.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QThread>
    3. #include <QHttp>
    4. #include <QTimer>
    5. #include <QUrl>
    6. #include <QHash>
    7. #include "MyThread.h"
    8. #include "Windows.h"
    9.  
    10. //----------------------------------------------------------------------------------
    11. MyThread::MyThread()
    12. {
    13. }
    14. //----------------------------------------------------------------------------------
    15. void MyThread::done( bool error ) // true if error occurs
    16. {
    17. if(error)
    18. {
    19. QHttp::Error err = m_pHttp->error();
    20. QString err_Str = m_pHttp->errorString();
    21. qDebug("HTTP done signal slot handler. Error: %s", err_Str.toAscii().data());
    22. }
    23. else
    24. qDebug("HTTP done signal slot handler. No Error.");
    25. }
    26.  
    27. //-------------------------------------------------------------------------------------------
    28. void MyThread::init(void)
    29. {
    30. m_Host = "169.254.177.84";
    31. m_UserName = "root";
    32. m_Password = "smokey";
    33. m_HttpPort = 80;
    34. m_SerialPort = 1;
    35.  
    36. m_HttpStateTable[QHttp::Unconnected] = "Unconnected";
    37. m_HttpStateTable[QHttp::HostLookup] = "HostLookup";
    38. m_HttpStateTable[QHttp::Connecting] = "Connecting";
    39. m_HttpStateTable[QHttp::Sending] = "Sending";
    40. m_HttpStateTable[QHttp::Reading] = "Reading";
    41. m_HttpStateTable[QHttp::Connected] = "Connected";
    42. m_HttpStateTable[QHttp::Closing] = "Closing";
    43.  
    44. }
    45. // -----------------------------------------------------------------------------
    46. void MyThread::initSignalsAndSlots(void)
    47. {
    48. connect(m_pHttp, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)),
    49. this, SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
    50. connect( m_pHttp, SIGNAL( requestStarted ( int ) ), this, SLOT( requestStarted( int ) ) );
    51. connect( m_pHttp, SIGNAL( requestFinished ( int , bool ) ), this, SLOT( requestFinished ( int , bool ) ) );
    52. connect( m_pHttp, SIGNAL( stateChanged ( int ) ), this, SLOT( stateChanged ( int ) ) );
    53. connect( m_pHttp, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
    54. this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
    55. connect( m_pHttp, SIGNAL( done ( bool ) ), this, SLOT( done (bool ) ) );
    56. }
    57.  
    58. // -----------------------------------------------------------------------------
    59. void MyThread::requestStarted( int id )
    60. {
    61. qDebug( "HTTP requestStarted slot handler for request type %s (id = %d)",
    62. m_httpRequestIdHashTable[id].toAscii().data(),id);
    63. int nothing = 0;
    64. nothing++;
    65. }
    66.  
    67. // -----------------------------------------------------------------------------
    68.  
    69. void MyThread::requestFinished( int id, bool error )
    70. {
    71. //qDebug( "HTTP requestFinished slot handler for request type %s (id = %d), error = %d",
    72. // m_httpRequestIdHashTable[id].toAscii().data(), id, error);
    73.  
    74. int nothing = 0;
    75. nothing++;
    76. QString err_Str = "No error";
    77. if( error )
    78. {
    79. QHttp::Error err = m_pHttp->error();
    80. err_Str = m_pHttp->errorString();
    81. nothing--;
    82. }
    83. qDebug( "HTTP requestFinished handler: error for request %s, error = %s",
    84. m_httpRequestIdHashTable[id].toAscii().data(), err_Str.toAscii().data());
    85. }
    86. // -----------------------------------------------------------------------------
    87. void MyThread::readResponseHeader(const QHttpResponseHeader &responseHeader)
    88. {
    89. # if 1
    90. switch (responseHeader.statusCode()) {
    91. case 200: // Ok
    92. //case 301: // Moved Permanently
    93. //case 302: // Found
    94. //case 303: // See Other
    95. //case 307: // Temporary Redirect
    96. {
    97. qDebug("HTTP Header status 200 (OK) found");
    98.  
    99. // See if there are pending requests
    100. if(m_pHttp->hasPendingRequests())
    101. qDebug("There are pending requests.");
    102. else
    103. qDebug("There are NO pending requests.");
    104.  
    105. // Get info on current ID
    106. int id = m_pHttp->currentId();
    107. qDebug( "Closing connection for request type %s",
    108. m_httpRequestIdHashTable[id].toAscii().data());
    109. //emit( abortHttpConnection() ); //??ejp This kills the TCP link and stops the video stream.
    110. m_pHttp->clearPendingRequests(); // clear pending request and start close request.
    111. id = m_pHttp->close(); // Tried close() but nothing happened.
    112. m_httpRequestIdHashTable.insert(id,"close");
    113. break;
    114. }
    115. default:
    116. //QMessageBox::information(this, tr("HTTP"),
    117. // tr("Download failed: %1.")
    118. // .arg(responseHeader.reasonPhrase()));
    119. break;
    120. }
    121. #endif
    122. }
    123. //----------------------------------------------------------------------------------
    124. void MyThread::slotAuthenticationRequired(const QString & hostname,
    125. quint16 port, QAuthenticator * authenticator)
    126. {
    127. qDebug( "Authentication req'd for host %s, User Name = %s, Password = %s",
    128. hostname.toAscii().data(), m_UserName.toAscii().data(), m_Password.toAscii().data() ); //??ejp debug
    129. authenticator->setUser( m_UserName );
    130. authenticator->setPassword( m_Password );
    131.  
    132. }
    133. //----------------------------------------------------------------------------------
    134. void MyThread::stateChanged( int state)
    135. {
    136. Q_ASSERT(state < 7); // There are only 7 states for QHTTP
    137. QString request = m_httpRequestIdHashTable[m_pHttp->currentId()];
    138. qDebug( "HTTP State for request type %s changed to %s",
    139. request.toAscii().data(), m_HttpStateTable[state].toAscii().data());
    140. }
    141.  
    142. //----------------------------------------------------------------------------------
    143. void MyThread::callbackFunc(void)
    144. {
    145. int id = m_pHttp->setHost( m_Host, QHttp::ConnectionModeHttp, m_HttpPort );
    146. m_httpRequestIdHashTable.insert(id,"setHost");
    147.  
    148. Sleep( 100); // wait for setHost to finish.
    149.  
    150. QString port_str = QString::number( m_SerialPort+1 );
    151. #if 0
    152. QString url_for_cmds = QString("/axis-cgi/com/serial.cgi?port=" + port_str );
    153. #else
    154. QString url_for_cmds = QString("/axis-cgi/mjpg/video.cgi");
    155. #endif
    156. id = m_pHttp->get(QUrl::toPercentEncoding(url_for_cmds) );
    157. m_httpRequestIdHashTable.insert(id,"GET");
    158.  
    159. id = m_pHttp->get(url_for_cmds );
    160. m_httpRequestIdHashTable.insert(id,"GET");
    161.  
    162. //while(1)
    163. // Sleep( 10);
    164.  
    165. //m_pHttp->get();
    166. };
    167.  
    168. //----------------------------------------------------------------------------------
    169. void MyThread::run(void)
    170. {
    171. init();
    172. m_pHttp = new QHttp();
    173. initSignalsAndSlots(); // Need m_pHttp initialized first!
    174.  
    175. QTimer::singleShot(1000, this, SLOT(callbackFunc())); // milli second timeout.
    176.  
    177. //m_pTimer = new QTimer(callback ;
    178.  
    179. QThread::exec ();
    180. //int res = QCoreApplication::exec();
    181.  
    182. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HTTP GET authentication does not proceed

    QHttp won't "rerequest" a failed request. It's a low-level mechanism, not a full blown solution. If you get an authentication error, it is your responsibility to set the user and password and send another request.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HTTP GET authentication does not proceed

    I'm confused then by the QT exaple in

    http://doc.trolltech.com/4.4/network-http.html

    I followed the QT example where they set the password in the slot connected to the QHttp signal authenticationRequired(). I copied the QT example slot function below.

    Qt Code:
    1. void HttpWindow::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator)
    2. {
    3. QDialog dlg;
    4. Ui::Dialog ui;
    5. ui.setupUi(&dlg);
    6. dlg.adjustSize();
    7. ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(hostName));
    8.  
    9. if (dlg.exec() == QDialog::Accepted) {
    10. authenticator->setUser(ui.userEdit->text());
    11. authenticator->setPassword(ui.passwordEdit->text());
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    When I looked at the example's header response handler below, it does not have any case for authentication required. So perhaps the example is incorrect? Should it have a case 401 Unauthorized? I understand what you are saying about handling the error manually, but I'd sure like to know how QT's example is supposed to work.

    Qt Code:
    1. void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader)
    2. {
    3. switch (responseHeader.statusCode()) {
    4. case 200: // Ok
    5. case 301: // Moved Permanently
    6. case 302: // Found
    7. case 303: // See Other
    8. case 307: // Temporary Redirect
    9. // these are not error conditions
    10. break;
    11.  
    12. default:
    13. QMessageBox::information(this, tr("HTTP"),
    14. tr("Download failed: %1.")
    15. .arg(responseHeader.reasonPhrase()));
    16. httpRequestAborted = true;
    17. progressDialog->hide();
    18. http->abort();
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Sample of Http server
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 30th November 2010, 23:43
  2. QHTTP does not see tunneled TCP HTTP OK authentication response
    By SailingDreams in forum Qt Programming
    Replies: 6
    Last Post: 23rd May 2009, 09:39
  3. A simple HTTP server going wrong.
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2008, 20:09
  4. uploading files to HTTP!!
    By Raajesh in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2008, 22:00

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.