免费试用:最火的人工智能查询工具
     最强AI人工智能聊天工具
     对话机器人解答疑难问题
星外智问直连原始API接口(GPT-4o)
➡️未登录受限条数,请登陆使用完整服务>>>➡️
请输入您要问的问题:


查下百度  查下必应

以下是单个问题的回复:

问:使用c++帮我写发送使用http或https使用get或post请求数据的函数
GPT答: 7i24.cn
以下示例使用libcurl库的HTTP POST请求(在此之前需要安装libcurl库):

```c++
#include <iostream>
#include <string>
#include <curl/curl.h>
 
using namespace std;
 
int main()
{
    CURL *hnd = curl_easy_init();
 
    curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(hnd, CURLOPT_URL, "http://example.com/api");
 
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "content-type:application/json");
    curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
 
    string data="{\"name\":\"Test\",\"phone_no\":\"1234567890\"}";
    curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, data.c_str());
 
    CURLcode ret = curl_easy_perform(hnd);
 
    curl_easy_cleanup(hnd);
    hnd = NULL;
 
    return 0;
}
```

此外,还可以使用libcurl实现HTTPS POST请求:
```c++
#include <iostream>
#include <string>
#include <curl/curl.h>
 
using namespace std;
 
int main()
{
    CURL *hnd = curl_easy_init();
 
    curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(hnd, CURLOPT_URL, "https://example.com/api");
 
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "content-type:application/json");
    curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
 
    curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
 
    string data="{\"name\":\"Test\",\"phone_no\":\"1234567890\"}";
    curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, data.c_str());
 
    CURLcode ret = curl_easy_perform(hnd);
 
    curl_easy_cleanup(hnd);
    hnd = NULL;
 
    return 0;
}
```

封装成函数:
```c++
#include <iostream>
#include <string>
#include <curl/curl.h>
 
using namespace std;
 
int HttpPost(string url, string header, string data)
{
    CURL *hnd = curl_easy_init();
 
    curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(hnd, CURLOPT_URL, url.c_str());
 
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, header.c_str());
    curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
 
    if(url.find("https") != string::npos)
    {
        curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
    }
 
    curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, data.c_str());
 
    CURLcode ret = curl_easy_perform(hnd);
 
    curl_easy_cleanup(hnd);
    hnd = NULL;
 
    return 0;
}
 
int main()
{
    string url = "http://example.com/api";
    string header = "Content-Type:application/json";
    string data = "{\"name\":\"Test\",\"phone_no\":\"1234567890\"}";
 
    return HttpPost(url, header, data);
}
```
一星  二星  三星  四星  五星 

互联网非法外之地,请不要输入不良内容
 查下百度 查下必应 02-22 21:45:36



chatGPT中国版--星外智问--8v6.CN
当前运行网址:haoadm.com
客服微信:xwgdd_com APP下载 怎么问 识别真假GPT4 使用GPT赚钱 退出登陆