libcurlppを使用しているときにHTTPレスポンスを抽出するにはどうすればよいですか。

StackOverflow https://stackoverflow.com/questions/5009113

  •  14-11-2019
  •  | 
  •  

質問

フォームを投稿して応答を得るためにlibcurlpp(a 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
.

このコードが実行されると、Verbosetrueに設定されているため、応答がSTDOUTに出力されることがわかります。しかし、それが標準的なものにダンプするのではなく、完全な返答にアクセスするにはどうすればよいですか?CURLPP :: Easyは、応答にアクセスする方法がありません。

グーグルでのヒットが同じ質問をしているが、返信はありません。CURLPPメーリングリストはデッドゾーンであり、Curlpp WebサイトのAPIセクションは1年間壊れています。

役に立ちましたか?

解決

これは私がついにそれをやった方法です:

// 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()にあるストリームで利用可能になりました。

他のヒント

質問が尋ねられてから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;
}
.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top