首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Restbed HTTP客户端空正文

Restbed HTTP客户端空正文
EN

Stack Overflow用户
提问于 2016-09-01 23:12:57
回答 2查看 1.4K关注 0票数 0

我使用C++和Restbed Library实现了一个REST客户端

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

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

我试着玩头球,但没有成功。

有图书馆知识的人能给我指个正确的方向吗?

或者给我推荐一个在C++上运行的简单易用的Solaris REST库?

示例:

代码语言:javascript
复制
/*
 * 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;
}

我当前的实现

代码语言:javascript
复制
#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( ) );
}
EN

回答 2

Stack Overflow用户

发布于 2016-11-13 17:02:51

第一个示例包含错误:

代码语言:javascript
复制
auto length = 0;

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

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

因此长度始终为0

票数 2
EN

Stack Overflow用户

发布于 2016-09-08 21:15:44

我切换到了Beast Library。它有较少的服务器端代码示例,但作为客户端实现,它已经足够了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39275257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档