Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Error: QIODevice::read (QDisabledNetworkReply): device not open on https request



  • Hello to everyone. I'm trying to send https request, and I have this code:
    void MainWindow::on_pushButton_clicked()
    {

        QByteArray data = "login=admin&password=123";
        QNetworkRequest qtr(QUrl("https://host"));
        qtr.setRawHeader("Accept","application/json");
        qtr.setRawHeader("header","key");
    
        QSslConfiguration config = QSslConfiguration::defaultConfiguration();
        config.setProtocol(QSsl::TlsV1_2);
        qtr.setSslConfiguration(config);
        reply = qnam.post(qtr,data);
        connect(reply, &QNetworkReply::finished, this, &MainWindow::request_finished);
        connect(reply, &QNetworkReply::sslErrors, this, &MainWindow::error_encured);
    

    }

    void MainWindow::request_finished() {
    qDebug() << "request finished!";
    QByteArray data = reply->readAll();
    qDebug() << data;
    reply->deleteLater();
    }

    Everything was fine, but now after finishing request, when I'm trying to read the answer, I have this error: QIODevice::read (QDisabledNetworkReply): device not open.
    This error on line reply->readAll();

    Thanks for any advice!



  • Actualy it works on localhost, but not to others


  • Lifetime Qt Champion

    Hi @Nabi,

    You are creating the network request on the stack here:

    QNetworkRequest qtr(QUrl("https://host"));

    so it will be destroyed when the function MainWindow::on_pushButton_clicked() finishes.

    I guess you need to create a member variable for your network request and you should be fine.

    Regards



  • Thanks for help!
    I made QNetworkRequest as member, but result: QIODevice::read(QDisabledNetworkReply): device not open


  • Lifetime Qt Champion

    hi @Nabi,

    please show your code.



  • Hi!
    It's interesting world, yesterday I tested everything, result same error, today I didn't change anything, run app and all right, it works:)))!
    This is my code:

    Header file:
    #include <QMainWindow>
    #include <QNetworkAccessManager>
    #include <QUrl>

    namespace Ui {
    class MainWindow;

    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();
    void request_finished();
    void error_occurred();

    private:
    Ui::MainWindow *ui;
    QUrl url;
    QNetworkRequest qtr;
    QNetworkAccessManager qnam;
    QNetworkReply *reply;
    };

    #endif // MAINWINDOW_H

    source file:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QtNetwork"
    #include <QUrl>
    #include <QDebug>
    #include <QSslConfiguration>
    #include <QtCore/QVariant>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {

        QByteArray data = "login=admin&password=123";
        qtr.setUrl(QUrl("https://host"));
        qtr.setRawHeader("Accept","application/json");
        qtr.setRawHeader("Content-Type","application/x-www-form-urlencoded");
        qtr.setRawHeader("key,"value");
    
        QSslConfiguration config = QSslConfiguration::defaultConfiguration();
        config.setProtocol(QSsl::TlsV1_2);
        qtr.setSslConfiguration(config);
        reply = qnam.post(qtr,data);
        connect(reply, &QNetworkReply::finished, this, &MainWindow::request_finished);
        connect(reply, &QNetworkReply::sslErrors, this, &MainWindow::error_occurred);
    

    }

    void MainWindow::request_finished() {
    qDebug() << "request finished!";
    QByteArray data = reply->readAll();
    qDebug() << data;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
    QJsonObject jsonObject = jsonDoc.object();
    QString token = jsonObject["key"].toString();
    if(token == "") {
    qDebug() << "Login password incorrect!";
    } else {
    qDebug() << "All right";
    }
    reply->deleteLater();
    }

    void MainWindow::error_occurred() {
    qDebug() << "error occurred";
    }

    If something wrong, please notice me!


  • Qt Champions 2019

    @Nabi You should cinnect a slot to http://doc.qt.io/qt-5/qnetworkreply.html#error-1 as well to see whether you get any errors



  • Hi!
    @jsulm I got this error: "Network access is disabled."


  • Qt Champions 2019

    @Nabi Does the server actually support/allow these requests?



  • @jsulm Yes! First I tested this request on Postman, all right. My request can't get access on remote hosts, on localhost all right. Sometimes it works, it seems me OS blocks app, but i don't have any antivirus and windows firewall (brandmauer) off.



  • I didn't change anything and today it works, I think sometimes actually OS blocks my application. But how to get access forever, actually port 443 and 80 (http and https) always open.
    I added this line code reply->deleteLater() to clear memory and I didn't send many requests, so OS should not block my application.
    Why sometimes OS blocks my application?
    I have installed OS Windows 8.1
    I think or i have some bugs, or I didn't handle some important errors, or I didn't add some important lines code to get access


Log in to reply