[Qt-interest] need some help with QNetworkAccessManager and authenticated http post.

wim.delvaux at adaptiveplanet.com wim.delvaux at adaptiveplanet.com
Sun Apr 5 05:49:26 CEST 2009


Have a look at this code please

URLPoster::URLPoster(const QString & F) : QObject() {

      manager = new QNetworkAccessManager(this);

      connect( manager,
              SIGNAL( finished(QNetworkReply*) ),
              this,
              SLOT( replyFinished(QNetworkReply*) )
            );
      connect( manager,
              SIGNAL( authenticationRequired(QNetworkReply*, QAuthenticator 
* ) ),
              this,
              SLOT( authenticate(QNetworkReply*, QAuthenticator * ) )
            );
      Friendly = F;
}

// sends POST request with data
void URLPoster::request( const QString & URL,
                         const QString & Data,
                         QEventLoop * EL ) {
      manager->post( QNetworkRequest( QUrl( URL ) ), Data.toUtf8() );

      Loop = EL;
}

// asks password
void URLPoster::authenticate( QNetworkReply* Reply, QAuthenticator * Auth ) {

      QDialog * Dlg = new QDialog();
      ...
	
      if( Dlg->exec() == QDialog::Accepted ) {
        Auth->setPassword( Password );
        Auth->setUser( UserID );
      }

      delete Dlg;
}

void URLPoster::replyFinished( QNetworkReply* Reply ) {

      int rv = Reply->error();

      printf( "Reply error %d %s\n",rv, qPrintable( Reply->errorString() ) );
      // stop waiting
      Loop->exit( rv );
}

and the 'main' code ( Where G->Network is a URLPoster *)

      QEventLoop * EL = new QEventLoop();

      G->Network->request( SomeURL,
                           QString( "Value=%1" ).arg(Data),
                           EL );

      free( Data );
      free( Translated );

printf( "EXEC\n" );
      // wait till comm finished
      int rv = EL->exec();
printf( "EXEC-END\n" );
      delete EL;

      if( rv != 0 ) {
        printf( "Error %d\n", rv );
        return -rv;
      }

      return 0;

What happens :

1. I see the EXEC print ( so the EL->exec() is active)
2. I see the HTTP POST request being sent (using wireshar)
3. I see the 401 reply requesting password
4. the dialog is shown

what I do NOT see is

1. a authentication reply being sent (nothin on wireshare)
2. the EXEC never finishes (i.e. the finished signal never fires)

What am i doing wrong ?
Thanx
W



More information about the Qt-interest-old mailing list