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

wim.delvaux at adaptiveplanet.com wim.delvaux at adaptiveplanet.com
Sun Apr 5 13:53:09 CEST 2009


Hi thiago

Thx for you reply.

I (still) use 4.4.3.  Would your suggestion work there too ? 

I tried this version but it still does not seem to send credentials to my 
php-based server.

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

      manager = new QNetworkAccessManager(this);

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

void URLPoster::request( const QString & URL,
                         const QString & Data,
                         QEventLoop * EL ) {
      DataToPost = Data;
      TheURL = URL;
      if( UserID.isEmpty() ) {
        // I cache the UserID to avoid sending the initing get every time
        manager->get( QNetworkRequest( QUrl( URL ) ) );
        connect( manager,
                SIGNAL( finished(QNetworkReply*) ),
                this,
                SLOT( replyFinished(QNetworkReply*) )
              );
      } else {
        // we already have credentials
        manager->post( QNetworkRequest( QUrl( URL ) ), DataToPost.toUtf8() );
      }

      Loop = EL;
}

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

      QDialog * Dlg = new QDialog();
...
      delete Dlg;
}

void URLPoster::replyFinished( QNetworkReply* Reply ) {

      if( DataToPost.size() ) {
        // was getting -> send data
        manager->post( QNetworkRequest( QUrl( TheURL ) ), DataToPost.toUtf8
() );
        DataToPost.clear();
        return;
      }

      int rv = Reply->error();

      QList<QByteArray> HL = Reply->rawHeaderList();

      for( int i = 0 ; i < HL.size(); i ++ ) {
        printf( "%s %s\n", HL[i].constData(),
        Reply->rawHeader( HL[i].constData() ).constData() );
      }

      // stop waiting
      Loop->exit( rv );
}

Thx
W

On Sunday 05 April 2009 08:38:58 Thiago Macieira wrote:
> wim.delvaux at adaptiveplanet.com wrote:
> >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 ?
>
> Nothing.
>
> POST and PUT with authentication doesn't work because the code has already
> discarded the data to be POST'ed. This is a bug and we're fixing it for
> 4.6.0 (since it requires a fairly big change to the internals and new
> attributes).
>
> In the meantime, for 4.5.x, send a GET to the same server that will cause
> the authentication to happen. Authentication credentials are stored and
> will be reused.





More information about the Qt-interest-old mailing list