我的qhttp get()调用在Windows上不起作用,但在Linux上起作用

时间:2009-04-29 16:05:03

标签: c++ qt get

我编写了一个使用qhttp来获取网页的程序。这在Linux上运行良好,但在我的Windows机器(Vista)上不起作用。似乎从未收到qhttp完成信号。

相关代码是:

    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!
 }

任何帮助都会被推荐。

马特

2 个答案:

答案 0 :(得分:1)

这根本不应该发生,即QHttp在Windows和Unix上都可以正常工作。

我的建议是检查服务是否给出了正确的答案。这可以通过例如完成。通过验证数据传输是否正常。您可以从QHttp的信号中追踪状态,例如dataReadProgressrequestStartedrequestFinished以及其他相关信号。

另一方面,为什么不使用推荐的QNetworkAccessManager而不使用旧的QHttp?为了让你的脚快速湿润,请查看我前一段时间发布到Qt Labs的示例:image viewer with remote URL drag-and-drop support。它使用上述QNetworkAccessManager从已删除的URL中获取图像。检查source-code,它只有150行。

答案 1 :(得分: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();
}