gpt4 book ai didi

c++ - Restbed HTTP 客户端空主体

转载 作者:行者123 更新时间:2023-11-28 02:01:44 25 4
gpt4 key购买 nike

我想用 C++ 和 Restbed 库实现一个 REST 客户端

我试图从示例中复制代码,但似乎遗漏了一些东西。

示例显示了主体,但我的代码没有。

我试过使用 Headers 但没有成功。

任何对图书馆有一定了解的人都可以指出正确的方向吗?

或者建议我一个在 Solaris 上运行的易于使用的 C++ REST 库?

示例:

/*
* Example illustrating a HTTP client.
*
* Usage:
* ./distribution/example/http_client
*/

#include <memory>
#include <future>
#include <cstdio>
#include <cstdlib>
#include <restbed>

using namespace std;
using namespace restbed;

void print( const shared_ptr< Response >& response )
{
fprintf( stderr, "*** Response ***\n" );
fprintf( stderr, "Status Code: %i\n", response->get_status_code( ) );
fprintf( stderr, "Status Message: %s\n", response->get_status_message( ).data( ) );
fprintf( stderr, "HTTP Version: %.1f\n", response->get_version( ) );
fprintf( stderr, "HTTP Protocol: %s\n", response->get_protocol( ).data( ) );

for ( const auto header : response->get_headers( ) )
{
fprintf( stderr, "Header '%s' > '%s'\n", header.first.data( ), header.second.data( ) );
}

auto length = 0;
response->get_header( "Content-Length", length );

Http::fetch( length, response );

fprintf( stderr, "Body: %.*s...\n\n", 25, response->get_body( ).data( ) );
}

int main( const int, const char** )
{
auto request = make_shared< Request >( Uri( "http://www.corvusoft.co.uk:80/?query=search%20term" ) );
request->set_header( "Accept", "*/*" );
request->set_header( "Host", "www.corvusoft.co.uk" );

auto response = Http::sync( request );
print( response );

auto future = Http::async( request, [ ]( const shared_ptr< Request >, const shared_ptr< Response > response )
{
fprintf( stderr, "Printing async response\n" );
print( response );
} );

future.wait( );

return EXIT_SUCCESS;
}

我目前的实现

#include "HttpClient.h"
#include "HTTPException.h"
#include <restbed>
#include <iostream>

using namespace std;
using namespace restbed;

void HttpClient::getVersion_0() {
auto request = make_shared< Request >( Uri( "http://www.golem.de/" ) );
request->set_header( "Accept", "*/*" );
request->set_header( "Cache-Control", "no-cache" );
request->set_header("Accept-Encoding", "gzip,deflate");
request->set_header("User-Agent", "PKG6/0.0.1");
request->set_header("Host", "www.golem.de");
request->set_header("Connection", "Keep-Alive");
request->set_method("GET");

auto response = Http::sync( request );

int code = response->get_status_code();
/*
if(code != 200){
throw pkg::exception::HTTPBadResponseException(code);
}
*/
print(response);

/*
auto future = Http::async( request, [ ]( const shared_ptr< Request >, const shared_ptr< Response > response )
{

} );

future.wait( );

*/
}


void HttpClient::print(const std::shared_ptr<restbed::Response> &response) {
fprintf( stderr, "\n*** Response ***\n" );
fprintf( stderr, "Status Code: %i\n", response->get_status_code( ) );
fprintf( stderr, "Status Message: %s\n", response->get_status_message( ).data( ) );
fprintf( stderr, "HTTP Version: %.1f\n", response->get_version( ) );
fprintf( stderr, "HTTP Protocol: %s\n", response->get_protocol( ).data( ) );

for ( const auto header : response->get_headers( ) )
{
fprintf( stderr, "Header '%s' > '%s'\n", header.first.data( ), header.second.data( ) );
}

auto length = 0;
response->get_header( "Content-Length", length );

Http::fetch( length, response );

fprintf( stderr, "Body: %.*s...\n\n", 25, response->get_body( ).data( ) );
}

最佳答案

第一个例子有错误:

auto length = 0;

response->get_header( "Content-Length", length );

get_header 实际上返回的是header值,第二个参数是默认值。

所以长度总是0

关于c++ - Restbed HTTP 客户端空主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39275257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com