#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Infos.hpp>
int main() {
try {
// 初始化curlpp
curlpp::initialize();
// 创建一个Easy对象
curlpp::Easy request;
// 设置请求的URL
request.setOpt(curlpp::options::Url("https://example.com"));
// 启用重定向
request.setOpt(curlpp::options::FollowLocation(true));
// 执行请求并获取响应
request.perform();
// 获取HTTP错误码
long httpCode = curlpp::infos::ResponseCode::get(request);
if (200 == httpCode || 201 == httpCode)
{
std::ostringstream response;
response << request;
}
else if (302 == httpCode || 303 == httpCode || 307 == httpCode)
{
// 获取重定向url
std::string redirectUrl;
curlpp::infos::EffectiveUrl::get(request, redirectUrl);
// 获取到重定向url后继续发起请求
......
}
// 清理curlpp
curlpp::terminate();
}
catch (curlpp::RuntimeError& e) {
std::cerr << e.what() << std::endl;
}
catch (curlpp::LogicError& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
curlpp(curl C++) http错误码和重定向后的url获取
最新推荐文章于 2024-11-11 21:48:36 发布