qt for macos 使用libcurl请求https协议

libcurl是一个非常实用的网络库,数据上传下传,登录等经常用,在Win开发的过程中使用的比较多,所以资源也比较多,在Mac上开发的比较少,而官网自带的库通常是不支持https的,本篇记录https的使用,首先使用

Macos libcurl静态库编译支持https_班公湖里洗过脚的博客-CSDN博客

这里编译好的支持https的libcurl静态库,Mac上由于签名的原因,一般使用静态库。

配置头文件目录和库依赖写在.pro文件中。

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++14

DESTDIR = ../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.0

SOURCES += \
    curlhttpsclient.cpp \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    curlhttpsclient.h \
    mainwindow.h

FORMS += \
    mainwindow.ui

macos:{
    INCLUDEPATH += $$PWD/curl
    INCLUDEPATH += $$PWD/openssl
    LIBS += -L$$PWD/libs -lssl -lcrypto -lcurl
    message("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 <curl.h>
#include <string>

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 <QDebug>

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 transfer
    curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, 120L);	// keep-alive idle time to 120 seconds
    curl_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 <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    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 <QDebug>
#include "curlhttpsclient.h"
#include <string>

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=============";
    //string strUrl = "https://www.csdn.net";
    string strUrl = "https://curl.se/download/";
    string strRes;
    CurlHttpsClient::getInstance()->httpsGetRequest(strUrl, strRes, 6);
    qDebug() << "MainWindow::slotHttpGet=======strRes======" << strRes.c_str();
}

void MainWindow::slotHttpPost()
{
    qDebug() << "MainWindow::slotHttpPost=============";
}

编译:

 添加

LIBS += -L$$PWD/libs -lssl -lcrypto -lcurl    
LIBS += -lz

运行,请求get

 上面是https://curl.se/download/网站的内容。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值