Simple http client demo

 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值