尝试使用 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 似乎没有任何方法来访问响应。

谷歌上有很多人问同样的问题,但没有回复。curlpp 邮件列表是一个死区,curlpp 网站的 API 部分已经被破坏了一年。

有帮助吗?

解决方案

这就是我最终做到的:

// 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().

其他提示

也许自提出问题以来,curpp 已经更新了。我正在使用在 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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top