【问题标题】:Example on how to do asynchronous http get request using cpp-netlib关于如何使用 cpp-netlib 进行异步 http get 请求的示例
【发布时间】:2012-10-25 00:36:37
【问题描述】:

我正在尝试使用 cpp-netlib 执行异步 http 请求。我在文档中找不到任何这样的示例,因此甚至无法编译它。我目前的尝试如下(在 cmets 中有编译错误)。任何提示如何使它工作?先感谢您!

#include <iostream>
#include <boost/network/protocol/http/client.hpp>

using namespace std;
using namespace boost::network;
using namespace boost::network::http;

typedef function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type; // ERROR: Expected initializer before '<' token

body_callback_function_type callback() // ERROR: 'body_callback_function_type' does not name a type
{
    cout << "This is my callback" << endl;
}

int main() {
    http::client client;
    http::client::request request("http://www.google.com/");
    http::client::response response = client.get(request, http::_body_handler=callback()); // ERROR: 'callback' was not declared in this scope
    cout << body(response) << endl;
    return 0;
}

【问题讨论】:

    标签: c++ asynchronous compiler-errors http-request cpp-netlib


    【解决方案1】:

    我没有使用过 cpp-netlib,但您的代码似乎存在一些明显的问题:

    第一个错误是函数 typedef 上缺少 boost::

    typedef function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type; // ERROR: Expected initializer before '<' token
    

    应该是

    typedef boost::function<void(boost::iterator_range<char const *> const &, boost::system::error_code const &)> body_callback_function_type;
    

    第二个错误是:

    body_callback_function_type callback() 
    {
        cout << "This is my callback" << endl;
    }
    

    应该是正确的函数:

    void callback( boost::iterator_range<char const *> const &, boost::system::error_code const &)
    {
        cout << "This is my callback" << endl;
    }
    

    第三个错误是你应该传递回调,而不是调用它:

    http::client::response response = client.get(request, http::_body_handler=callback());
    

    应该是

    http::client::response response = client.get(request, callback);
    

    希望这就是全部(或足以让您入门)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2018-12-05
      • 2015-08-12
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多