为什么qtnetworkaccessmanager不去authenticationRequired

时间:2013-03-29 11:14:13

标签: authentication qnetworkaccessmanager owncloud

我正在创建一个应用程序,提到连接到owncloud服务器上的一个实例,但我找不到为什么它没有连接到服务器。而不是那个回复我到达登录屏幕,我得到它的HTML代码

这是负责连接的代码

//the network request and reply
          QNetworkAccessManager * manager = new QNetworkAccessManager();
          QUrl url (url1);
          manager->get(QNetworkRequest(url));
          connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
                      SLOT(provideAuthenication(QNetworkReply*,QAuthenticator*)));
          connect(manager, SIGNAL(finished(QNetworkReply *)),
                  this, SLOT(result(QNetworkReply *)));

回复代码

void Login::result(QNetworkReply *reply)
{
    reply->deleteLater();

     if(reply->error() == QNetworkReply::NoError) {
         // Get the http status code
         int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
         if (v >= 200 && v < 300) // Success
         {
             qDebug()<<"Here we got the final reply";
             QString replyText = reply->readAll();
             qDebug()<<replyText;
         }
         else if (v >= 300 && v < 400) // Redirection
         {
             qDebug()<<"Get the redirection url";
             QUrl newUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
             // Because the redirection url can be relative,
             // we have to use the previous one to resolve it
             newUrl = reply->url().resolved(newUrl);

             QNetworkAccessManager *manager = reply->manager();
             QNetworkRequest redirection(newUrl);
             QNetworkReply *newReply = manager->get(redirection);
             QString replyText = newReply->readAll();
             qDebug()<<replyText;
             return; // to keep the manager for the next request
         }
     }
     else
     {
         // Error
         qDebug()<<reply->errorString();
     }

     reply->manager()->deleteLater();
 }
你可以帮我弄清楚为什么我得到登录屏幕而不是身份验证?

1 个答案:

答案 0 :(得分:0)

尝试在调用connect()之前调用manager->get(),否则当触发身份验证所需信号时,可能没有任何插槽可用于调用以处理该信号。

请改为尝试:

QNetworkAccessManager * manager = new QNetworkAccessManager();
QUrl url (url1);
connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
    SLOT(provideAuthenication(QNetworkReply*,QAuthenticator*)));
connect(manager, SIGNAL(finished(QNetworkReply *)),
    this, SLOT(result(QNetworkReply *)));
manager->get(QNetworkRequest(url));