C++ http get和post请求001

3 篇文章 0 订阅
2 篇文章 0 订阅

依赖:C++11、curl

使用C/C++简单实现http的get和post请求

/*
 * curl_easy_setopt(curl, CURLOPT_POST, 1L);  // 设置为Post
 * curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);  // 设置为Get
*/
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/system.h>

// POST 请求函数
int CurlPostFUNC(std::string deviceMId)
{
    char CurlHead_Msg[128] = "";
    deviceMId.copy(CurlHead_Msg, 128, 0);
    CURL *curl = NULL;
    CURLcode res;

    curl = curl_easy_init();
    if (curl == NULL)
    {
        return CURLE_FAILED_INIT;
    }

    struct curl_slist *headerlist = NULL;

    // 设置表头,表头内容可能不同
    headerlist = curl_slist_append(headerlist, "Content-Type:application/json");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

    // 设置URL
    curl_easy_setopt(curl, CURLOPT_URL, CurlHead_Msg);

    // 设置需要请求的参数,比如"{Param1=Content1&Param2=Content2...}"
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "Param1=Param1Content&Param2=Param2Content");
    // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, /* char 类型的字符数组 */);
    
    curl_easy_setopt(curl, CURLOPT_POST, 1L);  // 设置为Post
    // curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);  // 设置为Get

    // 发送
    res = curl_easy_perform(curl);
    {
        // 获取详细错误信息
        fprintf(stderr, "\033[31m curl_easy_perform() failed: %s\r\n \033[0m", curl_easy_strerror(res));
        printf("\n");
    }

    // 清空
    curl_easy_cleanup(curl);

    // 释放表头
    curl_slist_free_all(headerlist);

    return 0;
}

int main(int argc, char const *argv[])
{
    std::string deviceMId = "www.baidu.com";  //请求地址 URL
    std::cout << "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH -- 0" << std::endl;
    CurlPostFUNC(deviceMId);
    std::cout << "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH -- 1" << std::endl;

    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 中,可以使用第三方库比如 cURL 来实现 HTTP 请求,包括 GET 和 POST 请求。 以下是一个使用 cURL 实现 GET 请求的示例代码: ```c++ #include <curl/curl.h> #include <iostream> #include <string> int main() { CURL *curl; CURLcode res; std::string url = "https://www.example.com/"; curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } curl_easy_cleanup(curl); } return 0; } ``` 以上代码中,使用 cURL 初始化一个 CURL 对象,设置请求的 URL,然后调用 `curl_easy_perform` 函数来执行请求。如果请求成功,就可以从 `curl_easy_perform` 函数的返回值中获取响应数据。 以下是一个使用 cURL 实现 POST 请求的示例代码: ```c++ #include <curl/curl.h> #include <iostream> #include <string> int main() { CURL *curl; CURLcode res; std::string url = "https://www.example.com/"; std::string post_data = "key1=value1&key2=value2"; curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str()); res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } curl_easy_cleanup(curl); } return 0; } ``` 以上代码中,除了设置请求的 URL 之外,还需要设置 POST 请求的数据,使用 `curl_easy_setopt` 函数来设置 POST 数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值