以下是使用 ixWebSocket 库发送 HTTP 请求的示例代码:
#include <iostream>
#include <ixwebsocket/IXHttpClient.h>
int main() {
// 创建 HttpClient 对象
ix::HttpClient client;
// 设置请求参数
std::string url = "https://api.example.com/endpoint";
std::string body = "{\"key\": \"value\"}";
// 发送 POST 请求
auto response = client.post(url, body, {{"Content-Type", "application/json"}});
// 打印响应结果
if (response.error.has_value()) {
std::cout << "Request failed: " << response.error.value() << std::endl;
} else {
std::cout << "Response status code: " << response.statusCode << std::endl;
std::cout << "Response body: " << response.body << std::endl;
}
return 0;
}
这是一个简单的示例,你可以根据自己的需求进行适当修改。记得在编译和运行之前安装 ixWebSocket 库并将其包含到项目中。
内容由零声教学AI助手提供,问题来源于学员提问