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 }
时间: 2024-08-06 20:02:53

Simple http client demo的相关文章

A simple Test Client built on top of ASP.NET Web API Help Page

Step 1: Install the Test Client package Install the WebApiTestClient package from the NuGet Package Manager. Make sure to “Include Prerelease” then just type in “WebApiTestClient” and click Install. Once the package is installed, it will add the foll

c tcp sockt server client -- demo

server.c // // Created by gxf on 2020/2/6. // #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <stdio.h> #include <unistd.h> #include <pthread.h> #define BUFFSIZE 1024 void dealClientConnecti

c udp server client demo --待调试

server.c // // Created by gxf on 2020/2/7. // #include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdlib.h> #define BUFFSIZE 1024 int main(){ int serverSocketFd = socket(AF_INET, SOCK_DGRAM, 0); struct socka

kairosdb client库

The KairosDB client is a Java library that makes sending metrics and querying the KairosDB server simple. The HttpClient class is used to push metrics or query the KairosDB server. The library uses the builder pattern to simplify the task of creating

Advanced Rest Client调试RESTFul

Advanced REST client 基于浏览器的Rest Client工具 在chrome或者firefox浏览器都有很多插件,我一般都是使用chrome浏览器,在chrome的webstore中可以搜索到自己想要的插件.这里就讲讲Advance REST Client,Postman-REST Client,DEV HTTP CLIENT,Simple REST Client 网页开发者辅助程序来创建和测试自定义HTTP请求.它是一款非常强大,使用简单的客户端测试工具,得到了程序员的好评

《Python Network Programming Cookbook》读书笔记1---套接字, IPv4, 简单的Client/Server程序

这一部分主要介绍python中socket模块的相关内容,socket即套接字. socket是使用TCP/IP协议的应用程序通常采用的应用编程接口,它位于运输层和应用层之间,起源于UNIX,由于遵从UNIX“一切皆文件的”思想故socket可看作一种特殊的文件,对其的操作基本可以视为读写I/O.打开.关闭.关于套接字的基本概念@吴秦的Linux Socket编程(不限Linux)写的很详细,大家可以参考. 在下面列出的各个部分中我将先贴出代码,然后对其进行解释. 通过python3获得本机名和

A Simple OpenCASCADE Qt Demo-occQt

A Simple OpenCASCADE Qt Demo-occQt [email protected] Abstract. OpenCASCADE have provided the Qt samples in the samples directory, but they are a little complicated. So I decide write a simple OpenCASCADE Qt demo for the OpenCASCADE beginners. Key Wor

Deploy Ceph and start using it:simple librados cli

This part of the tutorial describes how to setup a simple Ceph client using librados (for C++). The only information that the client requires for the cephx authentication is Endpoint of the monitor node Keyring containing the pre-shared secret (we wi

REST client 基于浏览器的测试工具

以前在开发webservice服务,都是自己基于HTTP协议,自己写一个测试程序来进行测试,最近在研究RestFul,对以前webservice服务进行了重构,总结了不少经验,今天就给大家介绍下几款Rest Client的测试工具. REST介绍 所谓REST,是Representational State Transfer,这个词汇的中文翻译很不统一,而且很晦涩,有叫“具象状态传输”,有叫“表象化状态转变”,等等. REST风格的Web服务,是通过一个简洁清晰的URI来提供资源链接,客户端通过