简体   繁体   English

有关如何使用cpp-netlib执行异步http get请求的示例

[英]Example on how to do asynchronous http get request using cpp-netlib

I'm trying to do asynchronous http requests using cpp-netlib . 我正在尝试使用cpp-netlib进行异步http请求。 I couldn't find any examples of this in the documentation, as a result can't even get it to compile. 我在文档中找不到任何这样的例子,结果甚至无法编译。 My current attempt is below (with compilation errors in the comments). 我目前的尝试是在下面(评论中有编译错误)。 Any hints how to make it work? 任何提示如何使其工作? Thank you in advance! 先感谢您!

#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;
}

I've not used cpp-netlib, but it looks like there's some obvious issues with your code: 我没有使用过cpp-netlib,但看起来你的代码有一些明显的问题:

First error is a missing boost:: on the function typedef. 第一个错误是函数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

Should be 应该

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

Second error is: 第二个错误是:

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

Should be the right kind of function: 应该是正确的功能:

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

Third error is that you should pass the callback, not call it: 第三个错误是您应该传递回调,而不是调用它:

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

Should be 应该

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

Hopefully thats all (or enough to get you started). 希望这一切(或足以让你开始)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM