c++发送request请求代码
时间: 2023-08-03 07:02:48 浏览: 52
发送 HTTP 请求的代码可以使用 C++ 的网络库。以下是一个使用 cURL 库发送 GET 请求的示例代码:
```cpp
#include <iostream>
#include <curl/curl.h>
int main(int argc, char* argv[]) {
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 库来发送 GET 请求。首先,我们初始化了 cURL 句柄,然后设置请求的 URL。最后,我们调用 curl_easy_perform() 函数来执行请求。如果请求成功,函数将返回 CURLE_OK。如果请求失败,我们可以使用 curl_easy_strerror() 函数来获取错误信息。最后,我们清理 cURL 句柄并返回。
如果你需要发送 POST 请求或者带有请求头或请求体的请求,你可以使用不同的 cURL 选项来设置。具体的使用方法可以参考 cURL 库的官方文档。