Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple Http Get
QtWS25 Last Chance

Simple Http Get

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 974 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nico AE
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • N Offline
        N Offline
        Nico AE
        wrote on last edited by
        #3

        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()));
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          does connect return true?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved