Qt发送HTTP请求
Posted 酷熊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(); }
以上是关于Qt发送HTTP请求的主要内容,如果未能解决你的问题,请参考以下文章
使用 QNetworkAccessManager 在 QT 中的 HTTP POST 请求