【问题标题】:Boost.ASIO-based HTTP client library (like libcurl) [closed]基于 Boost.ASIO 的 HTTP 客户端库(如 libcurl)[关闭]
【发布时间】:2011-01-16 02:52:29
【问题描述】:

我正在寻找一个现代 C++ HTTP 库,因为 C++ 包装器很难解决 libcurl 的缺点。基于已成为事实上的 C++ TCP 库的 Boost.ASIO 的解决方案是首选。

【问题讨论】:

  • 嗨。只是想知道你最终使用了什么?我有一个类似的场景(需要 c++ http 客户端)。您最终是使用下面建议的 C++ 网络库还是其他库?谢谢。
  • 是的,我使用了 cpp-netlib。它相当简约(或 very 简约文档缺少功能),因此您必须手动执行 HTTP 基本身份验证等操作(添加适当的标头而不是指定登录名/密码)。如果他们继续开发,这将是非常好的,而且它目前肯定可以使用(并且仅作为标题是一个很大的优势)。
  • 作为记录,最近我遇到了 cpp-netlib 的问题,因为编译时间太长,即使是最简单的事情(使用 GCC)也需要超过 1 GB 的 RAM .所以至少将你的 get/post 函数隔离到一个单独的源模块中,你永远不需要修改或重新编译,并且不要在任何其他地方#include这个库,而不是在它的实现中。
  • 两年来发生了很多事情,显然 cpp-netlib 明显领先。它现在可以构建为一个库,避免了那些由仅头文件模式导致的过多编译时间。 API 现在支持基于 Boost.Thread 和 futures 的某种异步模式。还支持流式传输正文(对于大型文档很重要),API 仍然易于使用,而且这个库似乎拥有包中最大的功能集。
  • 我有一个小项目正在进行,我在空闲时间开发,但它处于早期阶段。 github.com/tghosgor/libashttp

标签: c++ http curl boost-asio


【解决方案1】:

前几天有人在another thread上推荐了这个:

http://cpp-netlib.github.com/

我认为这和你会发现的一样高级,但我不确定它是否足够成熟(我会说这可能是因为他们已经提议将它包含在 Boost 中)。

【讨论】:

  • 我花了相当多的时间才发现HTTPS还不支持。这显然还不成熟,如果会的话。
  • 不幸的是,如果它有效,那就太好了。它缺少很多功能,当我上次使用它时,它严重阻塞了分块响应。这让我非常难过,因为这个库的用户界面设计非常简单且非常受欢迎。
  • 这个库很简单,仅用于从 HTTP 服务器获取文件或构建您自己的简单 HTTP 服务器/客户端。但这正是我需要的! Libcurl 通常是矫枉过正。
  • 这个库如何方便地占用boost::network 命名空间也很有趣,同时与 Boost 没有任何关系,考虑到质量问题,也没有太多机会被 Boost 接受。跨度>
【解决方案2】:

迟到总比不到好,这是对旧问题的新答案。有一个名为 Boost.Beast 的新开源库,它使用 Boost.Asio 提供 HTTP 和 WebSocket 功能。它尽可能地模拟熟悉的 Asio 界面,并且有大量的文档。它使用 bjam 或 CMake 构建在 clang、gcc 和 Visual Studio 之上——您的选择!注意,我也是图书馆的作者。

https://github.com/boostorg/beast/

这是一个检索网页的完整示例程序:

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>

using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http;    // from <boost/beast/http.hpp>

// Performs an HTTP GET and prints the response
int main(int argc, char** argv)
{
    try
    {
        // Check command line arguments.
        if(argc != 4 && argc != 5)
        {
            std::cerr <<
                "Usage: http-client-sync <host> <port> <target> [<HTTP version: 1.0 or 1.1(default)>]\n" <<
                "Example:\n" <<
                "    http-client-sync www.example.com 80 /\n" <<
                "    http-client-sync www.example.com 80 / 1.0\n";
            return EXIT_FAILURE;
        }
        auto const host = argv[1];
        auto const port = argv[2];
        auto const target = argv[3];
        int version = argc == 5 && !std::strcmp("1.0", argv[4]) ? 10 : 11;

        // The io_context is required for all I/O
        boost::asio::io_context ioc;

        // These objects perform our I/O
        tcp::resolver resolver{ioc};
        tcp::socket socket{ioc};

        // Look up the domain name
        auto const results = resolver.resolve(host, port);

        // Make the connection on the IP address we get from a lookup
        boost::asio::connect(socket, results.begin(), results.end());

        // Set up an HTTP GET request message
        http::request<http::string_body> req{http::verb::get, target, version};
        req.set(http::field::host, host);
        req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

        // Send the HTTP request to the remote host
        http::write(socket, req);

        // This buffer is used for reading and must be persisted
        boost::beast::flat_buffer buffer;

        // Declare a container to hold the response
        http::response<http::dynamic_body> res;

        // Receive the HTTP response
        http::read(socket, buffer, res);

        // Write the message to standard out
        std::cout << res << std::endl;

        // Gracefully close the socket
        boost::system::error_code ec;
        socket.shutdown(tcp::socket::shutdown_both, ec);

        // not_connected happens sometimes
        // so don't bother reporting it.
        //
        if(ec && ec != boost::system::errc::not_connected)
            throw boost::system::system_error{ec};

        // If we get here then the connection is closed gracefully
    }
    catch(std::exception const& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

【讨论】:

【解决方案3】:

【讨论】:

  • 现在,在 2012 年,这个项目似乎已经死了,因为我可以在上面找到的页面自 2009 年以来就没有更新过。而且异步接口非常低级(如 asio),因此不是很方便使用的。非常少的功能,非常少的文档。
  • 是的,但它确实有效。
【解决方案4】:

您还应该查看 Pion 网络库:

http://pion.org/projects/pion-network-library

【讨论】:

  • 我终于对此有一个正确的了解,但它似乎只是服务器(尽管从文档中并不完全清楚)。非常少的文档,但至少代码似乎得到了积极维护。
  • 已经使用 Pion 库 8 个月了。主要使用Server组件,但也曾使用过它是一个客户端。两者都工作得很好。到目前为止遇到的唯一问题是 HttpResponse 类的最大内容大小 (1MB)。为了设置更高的限制,我们必须从 HttpResponse 派生。服务器坚如磐石。
【解决方案5】:

Boost.Http - 这里有一个新玩家:https://github.com/BoostGSoC14/boost.http,文档 - http://boostgsoc14.github.io/boost.http/

【讨论】:

  • 但是,到目前为止,这只是服务器端。
【解决方案6】:

有这个项目试图“提升”libcurl:https://github.com/breese/trial.url

我将以此作为设计 Boost.Http 客户端 API 的参考。但是,我计划专注于高级抽象,并尝试尽可能多地与 Beast.HTTP 作者合作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 2013-03-20
    • 2010-11-16
    • 2019-05-23
    • 1970-01-01
    • 2012-01-13
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多