1
votes

I am trying to read automatically some information from investing.com using QNetworkAccessManager. I can read from other sites but this site gives some webmaster tools which I want to access. https://www.investing.com/webmaster-tools/

I use this query which works in a browser. Here is my request code

class InvestingAPI: public QObject
{
    Q_OBJECT

    public:
       InvestingAPI();
       QueryTechnicals(QString Symbol, int TF1Minites);

    signals:
    //   void NewTechnicalSummary(int Timeframe, QString Symbol, QString Summary);
    private slots:
       void onData(QNetworkReply *reply);
    private:
      QNetworkAccessManager qnam ;

};

InvestingAPI::InvestingAPI()
{

connect(&qnam,SIGNAL(finished(QNetworkReply*)),this,SLOT(onData(QNetworkReply*));

connect(&qnam,SIGNAL(encrypted(QNetworkReply*)),this,SLOT(onData(QNetworkReply*)) );

}

InvestingAPI::QueryTechnicals(QString Symbol, int TF1Minites)
{
QString Query;
Query = "http://ssltsw.forexprostools.com/index.php?timeframe=300&lang=1&forex=1&commodities=8830,8836,8831,8849,8833,8862,8832&indices=175,166,172,27,179,170,174&stocks=334,345,346,347,348,349,350&tabs=1,2,3,4%22%20width=%22317%22%20height=%22467%22%3E%3C/iframe%3E%3Cdiv%20class=%22poweredBy%22%20style=%22font-family:arial,helvetica,sans-serif;%20direction:ltr;%22%3E%3Cspan%20style=%22font-size:%2011px;color:%20&selectedTabId=QBS_1";

QNetworkRequest Request;
Request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
connect(&qnam,SIGNAL(finished(QNetworkReply*)),this,SLOT(onData(QNetworkReply*)));
Request.setUrl(QUrl(Query));

Request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
qnam.get(Request);
}

And I have event

void InvestingAPI::onData(QNetworkReply *reply){
    // find data type
    // decode and return data to caller
    if(reply->error() != QNetworkReply::NoError){
        qDebug() << "Error";
        qDebug() << reply->errorString();
    }
    QString  html =  QString::fromUtf8(reply->readAll());
    qDebug() << html;
    QString SubData;
}

I do not get an error but I get an empty string rather than the html response.

Please help as I have no idea why this is not working here but is working in the browser.

1
Have you checked the response of the browser with a network monitor? Probably your browser makes a redirect or sends some headers or cookies. The first step is to reproduce the behavior with Postman.Thomas Sablik
I tried that thank you it seemed to come back with the exact result I was hoping to get in my program which is an HTML page. No cookieTrump Fan
Did you compare the requests and responses using a network monitor like Wireshark?Thomas Sablik

1 Answers

1
votes

By default Qt Network does not handle redirects like other tools, so that is why you get an empty data (if you check the "Location" header you will see the redirected url). In this case it is to enable the redirection:

Request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);