【问题标题】:Restbed HTTP Client Empty BodyRestbed HTTP 客户端空体
【发布时间】:2016-09-01 15:12:57
【问题描述】:

我用 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( ) );
}

【问题讨论】:

  • 我注意到您设置了“Accept-Encoding: gzip,deflate”标头。如果这是返回的压缩数据,您似乎没有解压缩,那么您很可能在正文中有 '\0' 字符。因此,当您尝试打印输出时,它无法显示任何内容。
  • 是否打印了任何输出?如果有,是什么?
  • 我试过没有标题。仅托管和接受内容标头。但无济于事。它打印(空)。打印标题和有关响应的信息。只是不是身体。
  • 您是否将示例中的 URL 更改为 www.corvusoft.co.uk 以查看会发生什么?
  • 是的,我尝试了很多组合。 Corvusoft 给了我一个 301,golem.de 给了我一个空的身体。

标签: c++ restbed


【解决方案1】:

第一个例子包含错误:

auto length = 0;

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

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

所以长度总是0

【讨论】:

    【解决方案2】:

    我切换到野兽图书馆。它的服务器端代码示例较少,但作为客户端实现就足够了。

    【讨论】:

    • 哦,听到这个消息很难过。我们决定将客户端功能提取到一个单独的项目 Restless 中。应该在 2016 年 11 月结束之前公开。野兽图书馆一切顺利,我们听到了好消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多