C++|Qt最简单的http的get请求
生活随笔
收集整理的這篇文章主要介紹了
C++|Qt最简单的http的get请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
運行截圖如下:
302重定向
QTcp
一般步驟
1.connectToHost連接服務器
2.響應connected信號,write數據
3.響應readyRead,read數據
?
代碼如下:
tcp.pro
?
QT += core network QT -= gui CONFIG += c++11 TARGET = tcp CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp \ client.cpp HEADERS += \ client.h?
client.h
?
#ifndef CLIENT_H #define CLIENT_H #include <QObject> #include <QTcpSocket> class Client : public QObject { Q_OBJECT public: explicit Client(QObject *parent = 0); ~Client(){} void startConnect(QString host,quint16 port); protected slots: void onConnected(); void onReadyRead(); private: QTcpSocket *m_socket; }; #endif // CLIENT_Hclient.cpp
?
#include "client.h" #include <QDebug> Client::Client(QObject *parent) : QObject(parent),m_socket(0) { } void Client::startConnect(QString host, quint16 port){ m_socket=new QTcpSocket(this); connect(m_socket,SIGNAL(connected()),this,SLOT(onConnected())); connect(m_socket,SIGNAL(readyRead()),this,SLOT(onReadyRead())); m_socket->connectToHost(host,port); } void Client::onConnected(){ m_socket->write("GET / HTTP/1.1\r\n\r\n"); } void Client::onReadyRead(){ QString msg=m_socket->readAll(); qDebug()<<msg; }main.cpp
?
#include <QCoreApplication> #include "client.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Client client; client.startConnect("www.baidu.com",80); return a.exec(); }?
總結
以上是生活随笔為你收集整理的C++|Qt最简单的http的get请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-ListWidget拖动(
- 下一篇: Qt工作笔记-QString中arg的使