使用libcurl请求https的get/post

最近有个需求,需要用c++请求下我自己的服务器,周末看了一下怎么发起http请求。

官方文档见:

https://curl.se/libcurl/c/example.html

官网的demo是基于c的,我用的时候报错了。下面是我写的get/post的方法,同步执行。

cpp 复制代码
namespace yeshen_http
{
  struct MemoryStruct
  {
    char *memory;
    size_t size;
  };

  static size_t
  WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
  {
    size_t realsize = size * nmemb;
    struct MemoryStruct *mem = (struct MemoryStruct *)userp;

    void *ptr = realloc(mem->memory, mem->size + realsize + 1);
    if (!ptr)
    {
      std::cout << "not enough memory (realloc returned NULL)" << std::endl;
      return 0;
    }
    mem->memory = (char *)ptr;
    memcpy(&(mem->memory[mem->size]), contents, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
    return realsize;
  }

  static const char *get_url = "https://yeshen.org";
}

int HTTP::get(const char *url, std::string &response)
{
  CURL *curl = curl_easy_init();

  struct yeshen_http::MemoryStruct chunk;
  chunk.memory = (char *)malloc(1);
  chunk.size = 0;

  if (curl)
  {
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, yeshen_http::WriteMemoryCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
    CURLcode res = curl_easy_perform(curl);
    int retCode = -1;
    if (res != CURLE_OK)
    {
      std::cerr << "curl_easy_perform() failed:" << curl << curl_easy_strerror(res) << std::endl;
    }
    else if (chunk.size == 0)
    {
      std::cout << (unsigned long)chunk.size << " bytes retrieved" << std::endl;
    }
    else
    {
      std::cout << (unsigned long)chunk.size << " bytes retrieved" << std::endl;
      response = chunk.memory;
      retCode = 0;
    }
    free(chunk.memory);
    curl_easy_cleanup(curl);
    return retCode;
  }
  return -1;
}

int HTTP::post(const char *url, const std::string &data)
{
  CURL *curl = curl_easy_init();
  if (curl)
  {
    const char *data_str = data.c_str();
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_str);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(data_str));
    CURLcode res = curl_easy_perform(curl);
    if (res != CURLE_OK)
    {
      std::cerr << "curl_easy_perform() failed:" << curl_easy_strerror(res) << std::endl;
      return -1;
    }
    curl_easy_cleanup(curl);
    return 0;
  }
  return -1;
}

cmake部分的处理

cmake 复制代码
target_link_libraries(${YESHEN_TARGET_NAME} PRIVATE 
    libcurl.so
)
相关推荐
earthzhang20213 小时前
《深入浅出HTTPS》读书笔记(7):安全的密码学Hash算法
网络·网络协议·http·https·1024程序员节
GGBondlctrl1 天前
丹摩征文活动 |【网络原理】关于HTTP的进化之HTTPS的加密原理的那些事
网络·https·非对称加密·对称加密·中间人攻击
胖头鱼不吃鱼-1 天前
HTTP和HTTPS的区别
网络协议·http·https
B.-2 天前
在 Flutter 应用中调用后端接口的方法
android·flutter·http·ios·https
wdxylb2 天前
将C++搭建的简单HTTP服务器升级为 HTTPS 服务器
运维·服务器·https
卜及中2 天前
HTTPS详解:加密机制、工作流程、CA证书与中间人攻击防护
网络协议·http·https
正小安2 天前
HTTP 和 HTTPS 的区别 - 2024最新版前端秋招面试短期突击面试题【100道】
前端·http·面试·https
踏雪Vernon3 天前
[DDNS][SSL][HTTPS]阿里云ACME Ubuntu22.04Server 安装教程
阿里云·https·ssl
earthzhang20214 天前
《深入浅出HTTPS》读书笔记(8):密码学Hash算法的分类
网络协议·算法·http·https·1024程序员节
yangshuo12814 天前
WordPress HTTPS 配置问题解决方案
网络协议·http·https·wordpress