Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Plik main.cpp:
- #include "mainwindow.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
- Plik mainwindow.h:
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QString>
- #include <QtNetwork/QNetworkAccessManager>
- #include <memory>
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- private slots:
- void urlConnect();
- void urlRead();
- void urlFinished();
- void urlSend();
- void sendFinished(QNetworkReply *r);
- private:
- std::unique_ptr<Ui::MainWindow> ui;
- QNetworkAccessManager *manager;
- QNetworkReply *reply;
- QString text;
- QString notify;
- QByteArray buffer;
- };
- #endif // MAINWINDOW_H
- Plik mainwindow.cpp:
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QtNetwork/QNetworkRequest>
- #include <QtNetwork/QNetworkReply>
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- manager = new QNetworkAccessManager;
- connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(urlConnect()));
- connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(urlSend()));
- }
- MainWindow::~MainWindow()
- {
- delete manager;
- }
- void MainWindow::urlConnect()
- {
- notify = QString("");
- QUrl url = ui->lineEdit->text();
- QNetworkRequest req(url);
- notify.append(QString("host: ") + req.url().host() + QString("\n"));
- notify.append(QString("path: ") + req.url().path() + QString("\n"));
- notify.append(QString("user: ") + req.url().userName() + QString("\n"));
- notify.append(QString("user: ") + req.url().userInfo() + QString("\n"));
- notify.append(QString("url: ") + req.url().url() + QString("\n"));
- notify.append(QString("str: ") + req.url().toString() + QString("\n"));
- notify.append(QString("file: ") + req.url().toLocalFile() + QString("\n"));
- reply = manager->get(req);
- text = QString("czytanie danych");
- ui->label_2->setText(text);
- connect(reply, SIGNAL(readyRead()), this, SLOT(urlRead()));
- connect(reply, SIGNAL(finished()), this, SLOT(urlFinished()));
- ui->textBrowser->setText(notify);
- }
- void MainWindow::urlRead()
- {
- text.append(".");
- if (text.length() > 25) text = QString("czytanie danych");
- ui->label_2->setText(text);
- buffer += reply->readAll();
- }
- void MainWindow::urlFinished()
- {
- text = QString("wczytano dane");
- ui->label_2->setText(text);
- QString a(buffer);
- notify.append(QString("dane: ") + a);
- ui->textBrowser->setText(notify);
- }
- void MainWindow::urlSend()
- {
- QString txt = ui->textEdit->toPlainText();
- QByteArray postData;
- postData.append(txt);
- QNetworkRequest request = QNetworkRequest(QUrl(ui->lineEdit->text()));
- request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
- manager->post(request, postData);
- connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(sendFinished(QNetworkReply*)));
- }
- void MainWindow::sendFinished(QNetworkReply *r)
- {
- disconnect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(sendFinished(QNetworkReply*)));
- if(r->error() == QNetworkReply::NoError)
- ui->textBrowser_2->setText("wysyłanie zakończone bez błędu");
- else
- ui->textBrowser_2->setText(QString("Błąd przy wysyłaniu: ") + r->errorString());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement