curlpp(curl C++) http错误码和重定向后的url获取

#include 
#include 
#include 
#include 

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;
}

你可能感兴趣的:(c++,http,开发语言)