• Qt发送HTTP请求


    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QNetworkReply>
    #include <QTimer>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void scanURLIsOK();
        void replyFinish(QNetworkReply*); //用于处理响应返回的数据
    
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
        QTimer* scanTimer;
    };
    
    #endif // MAINWINDOW_H

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        scanTimer = new QTimer(this);
        //connect( scanTimer, &QTimer::timeout, this, &MainWindow::scanURLIsOK );
        scanTimer->start( 1000*5 );  // for 100fps
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::scanURLIsOK()
    {
        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
        QNetworkRequest request;
    
        // 发送https请求前准备工作;
        QSslConfiguration config;
        QSslConfiguration conf = request.sslConfiguration();
        conf.setPeerVerifyMode(QSslSocket::VerifyNone);
        conf.setProtocol(QSsl::TlsV1SslV3);
        request.setSslConfiguration(conf);
    
        //request.setUrl(QUrl("http://www.urlshare.cn/umirror_url_check?url=http://www.fgggg.com"));
        request.setUrl(QUrl("http://www.hugedomains.com"));
        QNetworkReply* reply = manager->get(request);
        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinish(QNetworkReply*)));
    }
    
    void MainWindow::replyFinish(QNetworkReply *reply)
    {
        if(reply && reply->error() == QNetworkReply::NoError) {
            QByteArray data = reply->readAll();
            int len = data.size();
            QString s=QString::fromStdString(data.toStdString());
            qDebug() << s;
        } else {
            qDebug() << reply->errorString();
        }
        reply->close();
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        scanURLIsOK();
    }
  • 相关阅读:
    Seial port API and tool
    Simple HTTPD
    VC与Cygwin的结合
    zlib
    嵌入式开发系统编程文件格式解析
    ZB4O
    Wireshark基本介绍和学习TCP三次握手
    freeware
    Console2 A Better Windows Command Prompt
    iniparser
  • 原文地址:https://www.cnblogs.com/coolbear/p/7144856.html
Copyright © 2020-2023  润新知