2009-04-29 23 views
1

我寫了使用QHTTP得到一個網頁的程序。這在Linux上正常工作,但不能在我的Windows機器上運行(Vista)。看來,qhttp完成信號從來沒有收到。我QHTTP得到()調用不能在Windows工作,但確實在Linux上

相關的代碼是:

Window::Window() 
{ 
    http = new QHttp(this); 
    connect(http, SIGNAL(done(bool)), this, SLOT(httpDone(bool))); 
url = new QUrl("http://something.com/status.xml"); 
http->setHost(url->host(), url->port() != -1 ? url->port() : 80); 
    if (!url->userName().isEmpty()) http->setUser(url->userName(), url->password()); 
} 

void Window::retrievePage() 
{ 
byteArray = new QByteArray; 
result = new QBuffer(byteArray); 
result->open(QIODevice::WriteOnly); 

    httpRequestAborted = false; 
    httpGetId = http->get(url->path(), result); 
} 

void Window::httpDone(bool error) 
{ 
    //Never gets here! 
} 

任何幫助將appriecated。

馬特

+3

你能確保這是不是那些UAC問題之一?嘗試關閉UAC並讓我們知道您是否看到任何安全彈出窗口。 – dirkgently 2009-04-29 16:08:31

回答

1

這應該不會發生,即QHttp可以在Windows和Unix上可靠地工作。

我的建議是檢查是否發球給適當的響應。這可以通過例如通過驗證數據傳輸是否正常。您可以追蹤QHttp信號的狀態,例如dataReadProgress,requestStarted,requestFinished等相關信號。

在另一方面,而是採用老QHTTP,爲什麼不使用推薦QNetworkAccessManager呢?爲了讓你的腳快點溼,請查看我之前發佈給Qt Labs的示例:image viewer with remote URL drag-and-drop support。它使用上述QNetworkAccessManager來從丟棄的URL中獲取圖像。檢查source-code,它只有150行。

1

改寫由Ariya的建議使用QNetworkAccessManager看着this example

現在,這適用於Windows和Linux。

Window::Window() 
{ 
    connect(&manager, SIGNAL(finished(QNetworkReply*)), 
     this, SLOT(retrieveData(QNetworkReply*))); 
} 

void Window::retrieveMessage() 
{ 
    manager.get(QNetworkRequest(QUrl("http://..."))); 
} 

void Window::retrieveData(QNetworkReply *reply) 
{ 
    QVariant statusCodeV = 
    reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); 

    // "200 OK" received? 
    if (statusCodeV.toInt()==200) 
    { 
     QByteArray bytes = reply->readAll(); // bytes 
    } 

    reply->deleteLater(); 
} 
相關問題