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!
-
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
-
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_OBJECTpublic:
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!
-
@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
-
@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