• 使用libcurl请求https的get/post


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

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

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

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    cmake部分的处理

    target_link_libraries(${YESHEN_TARGET_NAME} PRIVATE 
        libcurl.so
    )
    
    • 1
    • 2
    • 3
  • 相关阅读:
    基于LSTM的天气预测 - 时间序列预测 计算机竞赛
    【SpringMVC】Controller中映射方法的参数解析过程
    Go语言的安装与环境配置
    JavaScript 生成随机颜色
    [含文档+PPT+源码等]精品微信小程序二手交易小程序+后台管理系统|前后分离VUE[包运行成功]计算机毕业设计项目源码Java毕设项目
    Sonar生成PDF错误Can‘t get Compute Engine task status.Retry..... HTTP error: 401
    wy的leetcode刷题记录_Day56
    如何使用 二次号查询API
    【2023美团后端-8】删除字符串的方案,限制不能连续删
    js中的原型链理解
  • 原文地址:https://blog.csdn.net/yeshennet/article/details/132655109