使用curl发送post请求,示例代码,返回结果正确:
#include <stdio.h>
#include <curl/curl.h>
int main(void) {
CURL *curl; CURLcode res;
// 初始化winsock的内容
curl_global_init(CURL_GLOBAL_DEFAULT);
//*获得一个curl的手柄(handle)
curl = curl_easy_init();
if(curl) {
/* 设置post请求地址,最好使用https,第三个参数是url,即网址 */
curl_easy_setopt(curl, CURLOPT_URL, “https://www.wangsansan.com/mydir/test/HttpsPostTest.php”);
//设置post传输的信息,第三个参数即传输的字符串
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, “{“A”:“111”, “B”:“222”}”);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, “curl_easy_perform() failed: %s\n”, curl_easy_strerror(res));
//结束curl_easy_cleanup 结束一个libcurl简易句柄 curl_easy_cleanup(curl); }
curl_global_cleanup(); return 0; }
上述代码是验证curl库的post通信功能,网址和验证信息是csdn中一位答主写的网站,如果收到post请求,则返回post发送的字符串信息。
验证网站:https://blog.youkuaiyun.com/byb123/article/details/84405533?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control
本文展示了如何在C++中利用curl库发送HTTPS POST请求。通过初始化curl、设置请求URL和POST数据,成功执行请求并检查返回结果,以此验证了curl库的post通信功能。示例代码中,请求的URL指向了一个优快云博主提供的验证接口,用于返回接收到的POST数据。
1万+

被折叠的 条评论
为什么被折叠?



