Qt发送HTTP请求

2021-06-28 03:05

阅读:675

标签:sign   network   else   byte   setup   let   ini   cpp   out   

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 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() errorString();
    }
    reply->close();
}

void MainWindow::on_pushButton_clicked()
{
    scanURLIsOK();
}

 

Qt发送HTTP请求

标签:sign   network   else   byte   setup   let   ini   cpp   out   

原文地址:http://www.cnblogs.com/coolbear/p/7144856.html


评论


亲,登录后才可以留言!