【问题标题】:how do I extract the http response when using libcurlpp?使用 libcurlpp 时如何提取 http 响应?
【发布时间】:2011-06-27 21:41:24
【问题描述】:

尝试使用 libcurlpp(libcurl 的 C++ 包装器)发布表单并获得响应。一切正常,但我不知道如何在 http 事务完成后以编程方式访问 curlpp::Easy 对象的响应。基本上:

#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
...
curlpp::Easy foo;
foo.setOpt( new curlpp::options::Url( "http://example.com/" ) );
foo.setOpt( new curlpp::options::Verbose( true ) );
...many other options set...
foo.perform();  // this executes the HTTP transaction

当此代码运行时,因为 Verbose 设置为 true 我可以看到响应得到输出到 STDOUT。但是我如何才能访问完整的响应而不是将其转储到 STDOUT? curlpp::Easy 似乎没有任何方法可以访问响应。

在 Google 中出现了很多点击,人们问同样的问题,但没有回复。 curlpp 邮件列表是一个死区,并且 curlpp 网站的 API 部分已经损坏了一年。

【问题讨论】:

    标签: libcurl curlpp


    【解决方案1】:

    这段代码也可以,它来自This Answer:

    #include <curlpp/cURLpp.hpp>
    #include <curlpp/Options.hpp>
    
    // RAII cleanup
    
    curlpp::Cleanup myCleanup;
    
    // Send request and get a result.
    // Here I use a shortcut to get it in a string stream ...
    
    std::ostringstream os;
    os << curlpp::options::Url(std::string("http://example.com"));
    
    string asAskedInQuestion = os.str();
    

    【讨论】:

      【解决方案2】:

      这是一个比其他一些示例更完整的示例。它借鉴了 Stephane 和 kometen 的答案,是我整理的代码,以确保我可以获得 HTTP 响应代码。

      void
      runBadQuery() {
          curlpp::Easy request;
          std::ostringstream stream;
      
          request.setOpt<curlpp::options::Url>("http://localhost:3010/bad");
          request.setOpt( new curlpp::options::WriteStream( &stream ) );
          request.perform();
      
          long http_code = curlpp::infos::ResponseCode::get(request);
          cout << "/bad got code: " << http_code << " And content: " << stream.str() << endl;
      }
      

      在我的示例服务器上运行时的输出是:

      /bad got code: 400 And content: {
        "error": "Illegal request"
      }
      

      (我的示例服务器返回 JSON。)

      【讨论】:

        【解决方案3】:

        也许 curlpp 自提出问题以来已更新。我正在使用在 example04.cpp 中找到的这个。

        #include <curlpp/Infos.hpp>
        
        long http_code = 0;
        request.perform();
        http_code = curlpp::infos::ResponseCode::get(request);
        if (http_code == 200) {
            std::cout << "Request succeeded, response: " << http_code << std::endl;
        } else {
            std::cout << "Request failed, response: " << http_code << std::endl;
        }
        

        【讨论】:

          【解决方案4】:

          这就是我最终做到的:

          // HTTP response body (not headers) will be sent directly to this stringstream
          std::stringstream response;
          
          curlpp::Easy foo;
          foo.setOpt( new curlpp::options::Url( "http://www.example.com/" ) );
          foo.setOpt( new curlpp::options::UserPwd( "blah:passwd" ) );
          foo.setOpt( new curlpp::options::WriteStream( &response ) );
          
          // send our request to the web server
          foo.perform();
          

          一旦foo.perform() 返回,完整的响应正文现在可以在WriteStream() 提供的流中使用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-03-11
            • 1970-01-01
            • 2019-07-31
            • 2014-01-16
            • 1970-01-01
            • 2013-06-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多