我使用C++和Restbed Library实现了一个REST客户端
我试图复制示例中的代码,但似乎遗漏了一些东西。
该示例显示了一个主体,但我的代码没有。
我试着玩头球,但没有成功。
有图书馆知识的人能给我指个正确的方向吗?
或者给我推荐一个在C++上运行的简单易用的Solaris 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( ) );
}发布于 2016-11-13 17:02:51
第一个示例包含错误:
auto length = 0;
response->get_header( "Content-Length", length );get_header实际上返回标题值,第二个参数是默认值。
因此长度始终为0
发布于 2016-09-08 21:15:44
我切换到了Beast Library。它有较少的服务器端代码示例,但作为客户端实现,它已经足够了。
https://stackoverflow.com/questions/39275257
复制相似问题