一上午的时间都是编译openssl,不过还是没有成功,很遗憾,这里整理了有关这个Demo的本件
网盘连接:见下方评论吧,长传太慢了
不过虽然没有编译通过openssl。不过也不影响使用,可以直接下载现成的openssl库进行开发,只需要安装,并且把文件拷贝值Qt目录的bin文件夹下面即可
下载地址:http://slproweb.com/products/Win32OpenSSL.html
剩下的就是按照网上的教程,一步一步的来编写代码
软件截图
详细代码
这里需要在工程文件中加入网络模块
pro工程文件
#------------------------------------------------- # # Project created by QtCreator 2016-12-27T09:16:58 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets network TARGET = http_Project TEMPLATE = app SOURCES += main.cpp\ widget.cpp HEADERS += widget.h FORMS += widget.ui RC_FILE = ./source/Logo/http_icon.rc
头文件
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkRequest> #include <QTimer> #include <QString> #include <QByteArray> #include <QTextCodec> #include <QMessageBox> #include <QUrl> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); bool sendRequest(const QString& strUrl); private slots: void slot_requestFinished(); void slot_requestTimeout(); void on_pushButton_clicked(); signals: void signal_requestFinished(bool bSuccess,const QString& strResult); private: Ui::Widget *ui; QString m_serUrl; QNetworkAccessManager* m_pNetworkManager; QNetworkReply* m_pNetworkReply; QTimer* m_pTimer; }; #endif // WIDGET_H
cpp文件
#include "widget.h" #include "ui_widget.h" #include <QDebug> const int nHTTP_TIME = 10000; Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); setWindowTitle("Http Demo"); //set window title ui->lineEdit_address->setToolTip("please input the address"); ui->lineEdit_address->setPlaceholderText("please input the address"); m_pNetworkManager = new QNetworkAccessManager(this); m_pNetworkReply = NULL; m_pTimer = new QTimer; connect(m_pTimer,SIGNAL(timeout()),this,SLOT(slot_requestTimeout())); } Widget::~Widget() { m_pTimer->stop(); delete ui; } bool Widget::sendRequest(const QString &strUrl) { m_serUrl = strUrl; QNetworkRequest netRequest; netRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-from-urlencoded"); netRequest.setUrl(QUrl(strUrl)); if(strUrl.toLower().startsWith("https")) { QSslConfiguration sslConfig; sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone); sslConfig.setProtocol(QSsl::TlsV1_1); netRequest.setSslConfiguration(sslConfig); } m_pNetworkReply = m_pNetworkManager->get(netRequest); connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); m_pTimer->start(nHTTP_TIME); qDebug()<<"sendRequest"; return true; } void Widget::slot_requestFinished() { m_pTimer->stop(); QByteArray resultContent = m_pNetworkReply->readAll(); QTextCodec* pCodec = QTextCodec::codecForName("UTF-8"); QString strResult = pCodec->toUnicode(resultContent); int nHttpCode = m_pNetworkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug()<<"mHttpCode"<<nHttpCode; if(nHttpCode == 200) { ui->textEdit_result->setText(strResult); emit signal_requestFinished(true,strResult); } else { emit signal_requestFinished(false,strResult); } m_pNetworkReply->deleteLater(); // this->deleteLater(); qDebug()<<"requestFinish"; } void Widget::slot_requestTimeout() { emit signal_requestFinished(false ,"timeout"); QMessageBox::warning(this,"Waring","Request False"); m_pNetworkReply->deleteLater(); // this->deleteLater(); } void Widget::on_pushButton_clicked() { QString str = ui->lineEdit_address->text().trimmed(); sendRequest(str); }