Qt http get request always returns status code 0 in multi thread mode requests



  • i have simple request method inside simple http client , that QRunnble worker is invoking all the returners of the request are status 0 , what more i spotted after few tests is, when i give the app to run with 1 thread that is
    only 1 url to process its working fine and i get status 200 . i suspect something in my http client code is worng and does not support multi thread mode here is my full httpclient code:

    this is the code of the request :

    @#ifndef HttpClient
    #define HttpClient
    #include <QNetworkAccessManager>
    #include <QtNetwork>
    #include <QUrl>

    QT_BEGIN_NAMESPACE
    class QSslError;
    class QAuthenticator;
    class QNetworkReply;
    QT_END_NAMESPACE

    class HttpClient : public QObject
    {
    Q_OBJECT
    public:
    HttpClient(QFile* file,QMutex* mutex);
    ~HttpClient();
    void startRequest(QString& url);
    public slots:
    #ifndef QT_NO_OPENSSL
    void sslErrors(QNetworkReply*,const QList<QSslError> &errors);
    #endif
    private:
    QString m_sUrl;
    QUrl m_url;
    QNetworkAccessManager* m_networkManager;
    QNetworkReply *reply;
    int httpGetId;
    void HandleNetworkError(QNetworkReply::NetworkError& networkError,
    QNetworkReply *networkReply);
    };

    #endif


    #include "HttpClient.h"
    #include <QMutexLocker>
    #include <QTextStream>

    HttpClient::HttpClient()
    {
    m_networkManager = new QNetworkAccessManager(this);
    }
    HttpClient::~HttpClient()
    {
    ;
    }

    void HttpClient::startRequest(QString& url)
    {
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    QEventLoop loop;
    reply = m_networkManager->get(request);
    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();
    LOG_MSG("Is UrlAlive?:"+url.toStdString())
    QString ApiResponse;
    QByteArray data=reply->readAll();
    ApiResponse.append(QString::fromUtf8(data));
    int iStatusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    if (reply->error()) {
    QNetworkReply::NetworkError networkError = reply->error();
    HandleNetworkError(networkError,reply);
    }
    QString s = QString::number(iStatusCodeV);
    reply->deleteLater();
    reply = 0;

    }
    void HttpClient::HandleNetworkError(QNetworkReply::NetworkError& networkError,QNetworkReply networkReply)
    {
    if(networkError != QNetworkReply::NoError)
    {
    QString err = networkReply->errorString();
    LOG_MSG("HttpClient::HandleNetworkError:"+err.toStdString());
    }
    }
    #ifndef QT_NO_OPENSSL
    void HttpClient::sslErrors(QNetworkReply
    ,const QList<QSslError> &errors)
    {

        reply->ignoreSslErrors();   
    

    }
    #endif@

    all of this called from QRunnble method that looks like this :

    @void ThreadWorker::run()
    {
    QMutexLocker lock(_m);
    startwork();
    lock.unlock();

    }

    void ThreadWorker::startwork()
    {
    m_pHttpClient = new HttpClient();
    //each thread gets unique url
    m_pHttpClient->startRequest(m_url);

    }@

    why its failing all the time?


Log in to reply
 

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