2013-03-29 74 views
1

我正在創建一個應用程序,提到連接到一個在owncloud服務器的實例,但我找不到爲什麼它不連接到服務器。而不是回覆我獲得登錄屏幕和我得到的html代碼爲它爲什麼qtnetworkaccessmanager不去認證需要

這是代碼負責連接

//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(); 
} 

可能你幫我弄清楚爲什麼我得到登錄屏幕,而不是身份驗證?

+0

它似乎是在URL中的問題! –

回答

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));