libcurl是一个非常实用的网络库,数据上传下传,登录等经常用,在Win开发的过程中使用的比较多,所以资源也比较多,在Mac上开发的比较少,而官网自带的库通常是不支持https的,本篇记录https的使用,首先使用
Macos libcurl静态库编译支持https_班公湖里洗过脚的博客-CSDN博客
这里编译好的支持https的libcurl静态库,Mac上由于签名的原因,一般使用静态库。
配置头文件目录和库依赖写在.pro文件中。
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++14DESTDIR = ../bin# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \curlhttpsclient.cpp \main.cpp \mainwindow.cppHEADERS += \curlhttpsclient.h \mainwindow.hFORMS += \mainwindow.uimacos:{INCLUDEPATH += $$PWD/curlINCLUDEPATH += $$PWD/opensslLIBS += -L$$PWD/libs -lssl -lcrypto -lcurlmessage("pwd=" + $$PWD)
}
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
源码如下:
#ifndef CURLHTTPSCLIENT_H
#define CURLHTTPSCLIENT_H#include
#include using namespace std;class CurlHttpsClient
{
public:static CurlHttpsClient* getInstance(){static CurlHttpsClient httpsClient;return &httpsClient;}public:CURLcode httpsGetRequest(const string& url, string& resp, int nTimeOut);CURLcode httpsPostRequest(const string& url, const string& postData, string& resp, int nTimeOut);private:CurlHttpsClient();~CurlHttpsClient();
};#endif // CURLHTTPSCLIENT_H
#include "curlhttpsclient.h"
#include static size_t writeCallback(void *buffer, size_t size, size_t nmemb, void* data) {std::string* s = (std::string*)data;s->append((char*)buffer, size * nmemb);return size * nmemb;
}CurlHttpsClient::CurlHttpsClient()
{curl_global_init(CURL_GLOBAL_DEFAULT);
}CurlHttpsClient::~CurlHttpsClient()
{}CURLcode CurlHttpsClient::httpsGetRequest(const string &url, string &resp, int nTimeOut)
{CURLcode res;void* handle = curl_easy_init();if (handle == NULL) {return CURLE_FAILED_INIT;}struct curl_slist* headers = NULL;headers = curl_slist_append(headers, "Connection: Keep-Alive");headers = curl_slist_append(headers,"content-type:application/json");curl_easy_setopt(handle, CURLOPT_ENCODING , "gzip");curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);curl_easy_setopt(handle, CURLOPT_URL, url.c_str());curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);curl_easy_setopt(handle, CURLOPT_TIMEOUT, nTimeOut);curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1L);curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writeCallback);curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void*)&resp);res = curl_easy_perform(handle);curl_easy_cleanup(handle);return res;
}CURLcode CurlHttpsClient::httpsPostRequest(const string& url, const string& postData, string& resp, int nTimeOut)
{CURLcode res;void* handle = curl_easy_init();struct curl_slist* headers = NULL;if (handle == NULL) {return CURLE_FAILED_INIT;}curl_easy_setopt(handle, CURLOPT_URL, url.c_str());headers = curl_slist_append(headers,"content-type:application/json;charset=UTF-8;");headers = curl_slist_append(headers,"Expect:");curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);curl_easy_setopt(handle, CURLOPT_POSTFIELDS, postData.c_str());curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, postData.size());curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, nTimeOut);curl_easy_setopt(handle, CURLOPT_TIMEOUT, nTimeOut);curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1); //支持服务器跳转curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L); // enable TCP keep-alive for this transfercurl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, 120L); // keep-alive idle time to 120 secondscurl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writeCallback);curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void*)&resp);curl_easy_setopt(handle, CURLOPT_POST, 1L);res = curl_easy_perform(handle);curl_slist_free_all(headers);curl_easy_cleanup(handle);return res;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:void initConnect();private slots:void slotHttpGet();void slotHttpPost();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include "curlhttpsclient.h"
#include using std::string;MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);initConnect();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::initConnect()
{connect(ui->pushButton_get, SIGNAL(clicked()), this, SLOT(slotHttpGet()));connect(ui->pushButton_post, SIGNAL(clicked()), this, SLOT(slotHttpPost()));CurlHttpsClient::getInstance();
}void MainWindow::slotHttpGet()
{qDebug() <<"MainWindow::slotHttpGet&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;";//string strUrl &#61; "https://www.csdn.net";string strUrl &#61; "https://curl.se/download/";string strRes;CurlHttpsClient::getInstance()->httpsGetRequest(strUrl, strRes, 6);qDebug() <<"MainWindow::slotHttpGet&#61;&#61;&#61;&#61;&#61;&#61;&#61;strRes&#61;&#61;&#61;&#61;&#61;&#61;" <}void MainWindow::slotHttpPost()
{qDebug() <<"MainWindow::slotHttpPost&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;";
}
编译&#xff1a;
![](https://img.php1.cn/3cd4a/1eebe/cd5/4283cd4bbba41b87.png)
添加
LIBS &#43;&#61; -L$$PWD/libs -lssl -lcrypto -lcurl
LIBS &#43;&#61; -lz
运行&#xff0c;请求get
![](https://img.php1.cn/3cd4a/1eebe/cd5/c72d78d7317a9e8e.webp)
上面是https://curl.se/download/网站的内容。