使用C++ AWS SDK的CurlHttpClient

这篇博客介绍了如何在C++应用程序中使用AWS SDK的CurlHttpClient类来发起HTTP请求。通过初始化API、创建HttpClient、构建HttpRequest并处理HttpResponse,展示了获取网页内容的完整流程。最后,清理资源并关闭API。
摘要由CSDN通过智能技术生成

       

#include <aws/core/http/curl/CurlHttpClient.h>

#include <aws/core/http/HttpRequest.h>

#include <aws/core/http/HttpResponse.h>

        Aws::String url = "http://www.csdn.net.cn";

        SDKOptions options;

        options.loggingOptions.logLevel = Utils::Logging::LogLevel::Error;

        InitAPI(options);

        Aws::Http::CurlHttpClient::InitGlobalState();

        auto client = Aws::MakeShared<Aws::Http::CurlHttpClient>("Alloc", Aws::Client::ClientConfiguration());

        auto uri = Aws::Http::URI(url);

        auto request = Aws::MakeShared<Aws::Http::Standard::StandardHttpRequest>("Alloc", uri, Aws::Http::HttpMethod::HTTP_GET);

        request->SetResponseStreamFactory(Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);

        auto response = client->MakeRequest(request);

        Aws::IOStream &retrieved_file = response->GetResponseBody();

        retrieved_file.seekg(0, retrieved_file.end);

        int length = retrieved_file.tellg();

        retrieved_file.seekg(0, retrieved_file.beg);

        char *content = new char[length + 1];

        memset(content, 0x00, length + 1);

        retrieved_file.read(content, length);

        SetConfig(conf, content);

        delete[] content;

        Aws::Http::CurlHttpClient::CleanupGlobalState();

        ShutdownAPI(options);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值