Simple http client demo

通沛
2023-12-01
 1 // httpclientdemo.h
 2 #ifndef HTTPCLIENTDEMO_H
 3 #define HTTPCLIENTDEMO_H
 4 
 5 #include <QMainWindow>
 6 #include <QTcpSocket>
 7 
 8 namespace Ui {
 9 class HttpClientDemo;
10 }
11 
12 class HttpClientDemo : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     explicit HttpClientDemo(QWidget *parent = 0);
18     ~HttpClientDemo();
19 
20 private:
21     Ui::HttpClientDemo *ui;
22     QTcpSocket *tcpSocket;
23     QString addr;
24 
25 private slots:
26     void onConnected();
27     void onReadyRead();
28     void onDisconnected();
29     void on_ClearButton_clicked();
30     void on_OKButton_clicked();
31 };
32 
33 #endif // HTTPCLIENTDEMO_H
 1 // httpclientdemo.cpp
 2 #include "httpclientdemo.h"
 3 #include "ui_httpclientdemo.h"
 4 
 5 #include <QDebug>
 6 #include <QFile>
 7 
 8 HttpClientDemo::HttpClientDemo(QWidget *parent) :
 9     QMainWindow(parent),
10     ui(new Ui::HttpClientDemo)
11 {
12     ui->setupUi(this);
13 
14     tcpSocket = new QTcpSocket(this);
15     connect(tcpSocket, SIGNAL(connected()), this, SLOT(onConnected()));
16     connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
17     connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
18 }
19 
20 void HttpClientDemo::onConnected()
21 {
22     qDebug() << "Connected..";
23     QString head = "GET / HTTP/1.1\r\n"
24                    "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"
25                    // Not to compress
26                    // "Accept-Encoding:gzip, deflate, sdch\r\n"
27                    "Accept-Language:zh-CN,zh;q=0.8\r\n"
28                    "Connection:keep-alive\r\n"
29                    "Host:";
30     head += addr + "\r\n"
31                    "Upgrade-Insecure-Requests:1\r\n"
32                    "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36\r\n\r\n";
33     QByteArray tmp = head.toLatin1();
34     tcpSocket->write(tmp.data());
35 }
36 
37 void HttpClientDemo::onReadyRead()
38 {
39     // Absolute path
40     QFile file("D:\\Workspace\\Qt\\HttpClientDemo\\index.txt");
41     file.open(QIODevice::Append | QIODevice::Text);
42     QTextStream out(&file);
43     qDebug() << "Ready read..";
44     out << tcpSocket->readAll();
45     out.flush();
46     file.close();
47 }
48 
49 void HttpClientDemo::onDisconnected()
50 {
51     qDebug() << "Disconnected..";
52 }
53 
54 HttpClientDemo::~HttpClientDemo()
55 {
56     delete ui;
57 }
58 
59 void HttpClientDemo::on_ClearButton_clicked()
60 {
61     ui->IpLineEdit->setText(QString());
62 }
63 
64 void HttpClientDemo::on_OKButton_clicked()
65 {
66     addr = ui->IpLineEdit->text();
67     tcpSocket->connectToHost(addr, 80);
68 }
 1 // main.cpp
 2 #include "httpclientdemo.h"
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     HttpClientDemo w;
 9     w.show();
10 
11     return a.exec();
12 }

转载于:https://www.cnblogs.com/yqzhpt/p/6339637.html

 类似资料:

相关阅读

相关文章

相关问答