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