QNetworkAccessManager无法正确执行

时间:2014-06-21 03:58:46

标签: c++ qt qnetworkaccessmanager

我正在尝试实现http://developer.nokia.com/community/wiki/Creating_an_HTTP_network_request_in_Qt并且我的代码编译没有错误但不起作用。我有一个警告,回复" QNetworkReply * reply = nam-> get(QNetworkRequest(url));"没有被使用。我确定这是我的错误,我没有正确设置HTTP GET,但我不确定如何纠正它。

// coreEng.h

#ifndef COREENG_H
#define COREENG_H
#include <QObject>
#include <QNetworkAccessManager>

class coreEng : public QObject
{
    Q_OBJECT
        public:
            //coreEng(QObject);
            coreEng(QObject *parent = 0) :
                QObject(parent)
                {
                    nam = new QNetworkAccessManager();
                }

        private slots:

        public slots:
            void connect();
            void url(QNetworkReply *reply);
            void finishedSlot(QNetworkReply* reply);
        private:
            QNetworkAccessManager* nam;
};

#endif // COREENG_H

// coreEng.cpp

"coreeng.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>

void coreEng::connect(){
    QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
    this, SLOT(finishedSlot(QNetworkReply*)));
}

void coreEng::url(QNetworkReply*){
    QUrl url("http://www.nyctelecomm.com");
    QNetworkReply* reply = nam->get(QNetworkRequest(url));
}

void coreEng::finishedSlot(QNetworkReply* reply){

    QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);

    if (reply->error() == QNetworkReply::NoError)
    {

        QImageReader imageReader(reply);
        QImage pic = imageReader.read();

        QByteArray bytes = reply->readAll();  // bytes
        QString string(bytes); // string
    }

    else
    {

    }

    //delete reply();

}

// main.cpp中

#include <QCoreApplication>
#include "coreeng.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    coreEng mConnect;
    mConnect.connect();

    return a.exec();
}

1 个答案:

答案 0 :(得分:0)

将void coreEng :: url(QNetworkReply *)更改为void coreEng :: url() 改变了虚URL(QNetworkReply *回复);取消url(); 添加了mConnect.url();到main.cpp,我可以看到带有wireshark的http数据包退出网卡并开始会话。