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. https:://query1... vs https:://...csv

https:://query1... vs https:://...csv

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 217 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.
  • M Offline
    M Offline
    MiC78
    wrote on last edited by
    #1

    Hi Guys,

    I am really having really troubles with Qtnetworkmanager when downloading data from this two sites:

    https://query1.finance.yahoo.com/v7/finance/download/TSLA?period1=1622246400&period2=1653782400&interval=1d&events=history&includeAdjustedClose=true

    https://cdn.cboe.com/resources/us/indices/vxocurrent.csv

    The latter goes fine, but the first one I cannot figure it out. Could you please help?
    My code is nothing fancy:

    https.h
    #ifndef HTTPS_H
    #define HTTPS_H

    #include <QtNetwork>

    class Https : public QObject
    {
    Q_OBJECT

    public:
    explicit Https(QObject *parent = nullptr);

    ~Https();

    protected:
    QString _data;
    struct status {
    bool successful;
    QString asStr;
    } _status;

    private:
    QNetworkAccessManager _networkAccessManager;
    QNetworkReply *_networkReply;
    QNetworkCookieJar *_cookieJar;

    public slots:
    void requestData(const QString &url);
    bool status() const;
    QString statusString() const;
    QString getData() const;

    private slots:
    void onReadyRead();
    void retreiveIncomingData();

    signals:
    // When the Https request is finished this signal is emitted so the client code can use it.
    void dataDelivered();
    };
    #endif // HTTPS_H

    https.cpp
    #include "Https.h"

    Https::Https(QObject *parent)
    : _data(""),
    _status({false, ""}),
    _networkAccessManager(parent),
    _networkReply(nullptr),
    _cookieJar(nullptr) {}

    Https::~Https() {

    };

    void Https::requestData(const QString &url)
    {
    _status.successful = false;
    _status.asStr = "Non-initiated.";
    _cookieJar = new QNetworkCookieJar(&_networkAccessManager);
    _networkAccessManager.setCookieJar(_cookieJar);
    QNetworkRequest request(url);
    request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
    _networkReply = _networkAccessManager.get(request);
    //_networkReply->setReadBufferSize(1024*1024);

    QObject::connect(_networkReply, &QNetworkReply::readyRead, this, &Https::onReadyRead);
    QObject::connect(_networkReply, &QNetworkReply::finished, this, &Https::retreiveIncomingData);
    }

    QString Https::getData() const
    {
    return _data;
    }

    void Https::onReadyRead()
    {
    _data.append(_networkReply->readAll());
    }

    void Https::retreiveIncomingData()
    {
    // If there are no errors...
    if (_networkReply->error() == QNetworkReply::NoError)
    {
    //Redirect? -> Cancel this reply and send new request with new URL
    int statusCode = _networkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    if (statusCode == 301 || statusCode == 302 || statusCode == 303 || statusCode == 305 || statusCode == 307 || statusCode == 308)
    {
    _data = "";

      QUrl dataUrl(_networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString());
      QNetworkRequest request(dataUrl);
      _networkReply = _networkAccessManager.get(request);
      _status.successful = false;
      _status.asStr = "Redirected...";
      return;
    }
    //qDebug() << "Size of received data: " << _data.length();
    
    // This pointer has to be deleted later. Check why by reading what deleteLater does!!
    _networkReply->deleteLater();
    
     _status.successful = true;
    _status.asStr = "Request successfully achieved.";
    
    //For debugging purposes
    printf(_data.toStdString().c_str());
    
    //emit a signal to be used by the client class
    emit dataDelivered();
    

    // If there are errors...
    }
    else
    {
    _data = "";
    _status.successful = false;
    _status.asStr = "Error!!.\n" +
    QString("Http protocol error number ") + QString::number(_networkReply->error()) + QString(".\n") +
    _networkReply->errorString();
    return;
    }

    }

    bool Https::status() const { return _status.successful;}

    QString Https::statusString() const { return _status.asStr;}

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What issue are you having ?
      Please apply proper formatting to the code you share, it's pretty unreadable as it is (the </> button or three backticks before and after the code).

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        I can retrieve either with curl. There are no redirections, so these should be trivial transfers once a connection is made. There is a slight SSL difference. The first URL ends up with "SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256". The second URL ends up with " SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384".

        Connect to the QNetworkReply::sslErrors() signal to see if the initial SSL connection is the problem

        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