C++ 使用 Curl 库执行 XHR 请求并发送数据
要在 C++ 中执行带有数据的 XHR 请求,您需要使用一个 HTTP 客户端库,例如 Curl 或 WinHTTP。下面是一个使用 Curl 库的示例代码:
#include <curl/curl.h>
int main() {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
// 设置请求的 URL
curl_easy_setopt(curl, CURLOPT_URL, 'http://example.com');
// 设置请求的数据
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, 'data=hello');
// 执行请求
res = curl_easy_perform(curl);
// 检查请求是否成功
if (res != CURLE_OK) {
fprintf(stderr, 'curl_easy_perform() failed: %s\n', curl_easy_strerror(res));
}
// 清理 Curl 句柄
curl_easy_cleanup(curl);
}
return 0;
}
在上面的代码中,我们使用 curl_easy_setopt 函数设置请求的 URL 和数据。然后,我们使用 curl_easy_perform 函数执行请求。最后,我们使用 curl_easy_cleanup 函数清理 Curl 句柄。
原文地址: https://www.cveoy.top/t/topic/kfAx 著作权归作者所有。请勿转载和采集!