使用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
)
相关推荐
pingxiaozhao7 小时前
在Ubuntu内网环境中为Gogs配置HTTPS访问(通过Apache反向代理使用IP地址)
ubuntu·https·apache
小徐Chao努力15 小时前
【安全】加密算法原理与实战
安全·https·des·ssl·加密·aes·rsa
遇见火星2 天前
nginx或tengine服务器,配置HTTPS下使用WebSocket的线上环境实践!
服务器·websocket·nginx·https·tengine
gs801402 天前
PEM格式证书 = 域名证书.crt + 根证书(root_bundle).crt 含义解析
服务器·https·ssl
游戏开发爱好者82 天前
Charles的安装和使用教程
websocket·网络协议·tcp/ip·http·网络安全·https·udp
李詹2 天前
HTTPS为何仍有安全漏洞?解析加密协议下的攻击面
网络·网络协议·https
被一米六支配的恐惧2 天前
nginx配置ssl证书,实现https安全访问.
nginx·https·ssl
星星跌入梦境*2 天前
前端面试题(六):HTTP和HTTPS的区别以及他们如何保障数据安全
网络协议·http·https
秋叶清风3 天前
nginx 代理 https 接口
网络·nginx·https
BillKu3 天前
本地项目HTTPS访问问题解决方案
网络协议·http·https