RESTful Client API是一种基于HTTP协议与RESTful服务交互的客户端实现方式。在C++中,它允许程序通过HTTP请求(GET、POST、PUT、DELETE等)与远程服务器交换数据,通常使用JSON或XML格式。
微软开发的跨平台库,支持异步操作:
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
void make_rest_call() {
http_client client(U("https://api.example.com"));
client.request(methods::GET, U("/resource/1"))
.then([](http_response response) {
return response.extract_string();
})
.then([](std::string body) {
std::cout << "Response: " << body << std::endl;
});
}
经典C库的C++封装:
#include <curl/curl.h>
#include <iostream>
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
size_t total_size = size * nmemb;
output->append((char*)contents, total_size);
return total_size;
}
void curl_rest_call() {
CURL* curl = curl_easy_init();
if(curl) {
std::string response;
curl_easy_setopt(curl, CURLOPT_URL, "https://api.example.com/resource/1");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
CURLcode res = curl_easy_perform(curl);
if(res == CURLE_OK) {
std::cout << "Response: " << response << std::endl;
}
curl_easy_cleanup(curl);
}
}
高性能库,适合需要精细控制的情况:
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <iostream>
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = net::ip::tcp;
void beast_rest_call() {
try {
net::io_context ioc;
tcp::resolver resolver(ioc);
beast::tcp_stream stream(ioc);
auto const results = resolver.resolve("api.example.com", "443");
stream.connect(results);
http::request<http::string_body> req{http::verb::get, "/resource/1", 11};
req.set(http::field::host, "api.example.com");
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
http::write(stream, req);
beast::flat_buffer buffer;
http::response<http::dynamic_body> res;
http::read(stream, buffer, res);
std::cout << "Response: " << beast::buffers_to_string(res.body().data()) << std::endl;
beast::error_code ec;
stream.socket().shutdown(tcp::socket::shutdown_both, ec);
}
catch(std::exception const& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
原因:自签名证书或证书链不完整 解决:
// 对于libcurl,可以临时禁用验证(不推荐生产环境)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
// 更好的方案是指定CA证书路径
curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/cacert.pem");
原因:网络延迟或服务端响应慢 解决:
// cpprestsdk设置超时
http_client_config config;
config.set_timeout(std::chrono::seconds(30));
http_client client(U("https://api.example.com"), config);
// libcurl设置超时
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
原因:未正确释放资源 解决:
// 确保所有资源都有对应的释放
CURL* curl = curl_easy_init();
// ... 使用curl ...
curl_easy_cleanup(curl); // 必须调用
原因:字符集不匹配 解决:
// 明确指定编码
req.set(http::field::content_type, "application/json; charset=utf-8");
选择适合的库取决于项目需求:cpprestsdk适合现代C++项目,libcurl适合需要广泛兼容性的场景,Boost.Beast适合需要高性能和精细控制的场合。
没有搜到相关的文章