Simple Http Get



  • Hello guys, I have a problem. I try get simple .txt content from my localhost "http://localhost/api/v1.txt" but I don't receive any signal from my connections.

    //your code here
    #ifndef REQUESTS_HPP
    #define REQUESTS_HPP
    
    #include <QObject>
    
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QNetworkRequest>
    #include <QUrl>
    #include <QUrlQuery>
    
    class Requests : public QObject
    {
        Q_OBJECT
    public:
        explicit Requests(QObject *parent = 0);
        ~Requests()
        {
            delete m_reply;
        }
    
        void    setUrlServer(const QString& s) { urlServer = s; }
        void    setPostQueryData(const QString& key, const QString& value);
    
        void    requestByGet();
    signals:
    
    public slots:
        void    replyFinished(QNetworkReply* rply);
    
        void    rGet_read();
        void    rGet_end();
        void    rErrors(QNetworkReply::NetworkError errorT);
        void    rMetaChange();
        void    rRedictActivated(QUrl url);
        void    rSSlErrorsDetected(QList<QSslError> errors);
    
    private:
        QUrlQuery       hpost_dataQuery;
        QUrl            urlServer;
        // -- NETWORK
        QNetworkAccessManager   m_manager;
        QNetworkReply           *m_reply;
    };
    
    #endif // REQUESTS_HPP
    
    

    Implementation

    //your code here
    #include "requests.hpp"
    
    Requests::Requests(QObject *parent) : QObject(parent)
    {
    
        m_reply = nullptr;
    
        //connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    
    }
    
    void Requests::setPostQueryData(const QString &key, const QString &value)
    {
        hpost_dataQuery.addQueryItem(key, value);
    }
    
    void Requests::requestByGet()
    {
        QNetworkRequest __request;
    
        __request.setUrl(urlServer);
    
        m_reply = m_manager.get(__request);
        qDebug() << "getting from url: " << urlServer.toString();
    
        connect(m_reply, SIGNAL(readyRead()), SLOT(rGet_read()));
        connect(m_reply, SIGNAL(finished()), SLOT(rGet_end()));
        connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(rErrors(QNetworkReply::NetworkError)));
        connect(m_reply, SIGNAL(metaDataChanged()), SLOT(rMetaChange()));
        connect(m_reply, SIGNAL(redirected(QUrl)), SLOT(rRedictActivated(QUrl)));
        connect(m_reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(rSSlErrorsDetected(QList<QSslError>)));
    }
    
    void Requests::replyFinished(QNetworkReply *rply)
    {
        if(rply->error() == QNetworkReply::NoError)
        {
            int http_statusCode = rply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toUInt();
    
            if(http_statusCode >= 200 && http_statusCode < 300)
            {
                if(rply->isReadable())
                {
                    QString ReplyString = QString::fromUtf8(rply->readAll());
    
    #ifdef _DEBUG
                    qDebug() << ReplyString;
    #endif
                }
            }
            else if(http_statusCode >= 300 && http_statusCode < 400)
            {
                // Get the redirection url
                QUrl NewUrl = rply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
    
                // Because the redirection url can be relative, we have to use
                // prevous one to resolve it
                NewUrl = rply->url().resolved(NewUrl);
    
                QNetworkAccessManager *tManager = rply->manager();
                QNetworkRequest tRedirection(NewUrl);
    
                tManager->get(tRedirection);
    
                return; // to keep the manager for the next request
            }
            else
            {
                qDebug() << "Error on reply finished.";
            }
        }
    
        rply->deleteLater();
    }
    
    void Requests::rGet_read()
    {
        qDebug() << "rr";
    }
    
    void Requests::rGet_end()
    {
        qDebug() << "fim\n";
    }
    
    void Requests::rErrors(QNetworkReply::NetworkError errorT)
    {
        qDebug() << errorT;
    }
    
    void Requests::rMetaChange()
    {
        qDebug() << "Meta changed...";
    }
    
    void Requests::rRedictActivated(QUrl url)
    {
        qDebug() << "Received redirect: " << url.toString();
    }
    
    void Requests::rSSlErrorsDetected(QList<QSslError> errors)
    {
        qDebug() << "Received ssl errors";
    }
    
    

    Someone have any idea why i don't receive any signal?



  • You are missing the receiver argument in connects.
    connect(m_reply, SIGNAL(readyRead()), SLOT(rGet_read())); should become connect(m_reply, SIGNAL(readyRead()), this,SLOT(rGet_read()));
    if you used the new connect syntax it would detect this kind of things at compile time



  • Unfortunaly no, i changed my connections and not receive any signal.

    //your code here
    connect(m_reply, SIGNAL(readyRead()), this, SLOT(rGet_read()));
        connect(m_reply, SIGNAL(finished()), this, SLOT(rGet_end()));
    


  • does connect return true?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.