V
V
volia72016-05-25 01:06:08
Qt
volia7, 2016-05-25 01:06:08

How to send http request using QNerworkAccessManager in c++?

Hello, I am writing a C ++ program in qtcreator, I need to get data from the server, for which I am trying to use qnetworkaccessmanager, I registered the header files, took the code from the example, but the creator swears at the code,
QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http: //qt.nokia.com")));
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
neosapient, 2016-05-27
@volia7

Who knows, maybe your code works except for one small mistake - the url like 'http://qt.nokia.com' no longer exists. Try accessing ' www.qt.io '
net_test.pro

QT += core
QT -= gui
QT += network
CONFIG += c++11

TARGET = net_test
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    test.cpp

HEADERS += \
    test.h

test.h
#ifndef QXTEST_H
#define QXTEST_H

#include <QObject>

class QNetworkReply;

class QxTest: public QObject
{
    Q_OBJECT
public:
    QxTest();
public slots:
    void replyFinished(QNetworkReply* rep);
};

#endif // QXTEST_H

test.cpp
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QNetworkAccessManager>
#include "test.h"

QxTest::QxTest()
{
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));

    manager->get(QNetworkRequest(QUrl("http://www.qt.io")));

}

void QxTest::replyFinished(QNetworkReply* rep)
{
    qDebug() << rep->readAll();
}

main.cpp
#include <QCoreApplication>
#include "test.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QxTest test;
    return a.exec();
}

R
Rou1997, 2016-05-25
@Rou1997

What exactly is he complaining about? Look at the error log in the Problems window.
Do you have a replyFinished slot (handler)? He, too, should have been taken as an example.
If it “swears” at the QNetworkAccessManager classes themselves, etc., then you need to connect the appropriate include, find which ones on the Internet, and if it “swears” at include, then, in turn, you need to add the library (“lib”) in the . pro

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question